How can I join plugin data with entry data based on uri? I have a plugin with a record (and model). The record have attributes uri and hits. The uri attribute in my record corresponds to an entry's uri, as stored in the craft_elements_i18n table. 

What is the most efficient way to return entries with the uris I have stored, sorted on hits? Is there a way to join the uri attribute in my record with the uri in the craft_elements_i18n table, and return entry data and hits in one go?

Right now I have this, which does the job but isn't very efficient:

$results = $this-&gt;queryResultRecord-&gt;getQueryResultsBySetId($querySet-&gt;id);

if ($results) {
    foreach ($results as $result) {
        $criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
        $criteria-&gt;uri = $result-&gt;uri;
        $entries = $criteria-&gt;find();

        if ($entries) {
            $entry = $entries[0];
            $return[] = array('entry'=&gt;$entry, 'hits'=&gt;$result-&gt;hits);
        } 
    }
}


The getQueryResultsBySetId method return the data from my record sorted by hits. The $return variable is returned back to my frontend templates where I can do:

{% for result in results %}
    {{ result.entry.title }} - {{ result.hits }}&lt;br&gt;
{% endfor %}
You could of course manually create a select query with a join that fetches entries ordered by hits descending, however you will basically be recreating what $criteria->find() does above. I imagine this will introduce an almost negligible performance improvement, but it really depends on how many entries you are returning. 

The following code should give you a starting point:

$query = craft()-&gt;db-&gt;createCommand()
    -&gt;from('pluginTable pluginTable')
    -&gt;join('elements_i18n elements_i18n', 'elements_i18n.uri = pluginTable.uri')
    -&gt;join(...)
    -&gt;order('pluginTable.hits desc');