Hi everyone,
I have a table that has about 500000 rows.
Ok so there is a ITEM table and a TAG table, and an ITEM-TAG mapping table.
Tags and Items have an M:N relationship.
My Problem right now:
When i do a select item.id, item.name, item.owner, item.datecreated, from the tables, it is possible that I get multiple ITEMID because each item can have more than one tag mapped to it.
As such, in order to get a list of distinct items, my query is like this:
SELECT DISTINCT(item.id), name, owner, datecreated.
from item
LEFT JOIN itemtagmap ON (item.id = itemtagmap.itemid)
WHERE 1
(AND item.owner=???? AND itemtagmap.tagid IN (???,????) )
ORDER BY item.datecreated
The problem is now that because of the distinct, the query is darned slow.
Things speed up dramatically if i drop the distinct requirement. But that will totally screw up my pagination.
I have a multi column index in this order:
item.datecreated, item.owner, item.id,
It works absolutely fine as long as i don't have the DISTINCT.
The reason why i need to left join the ITEM to ITEMTAGMAP in the first place is because i have some code that allows me to append on additional WHERE itemtagmap.tagid = XXX clauses, so there has to be a join.
Would appreciate any help.... Many thanks...
I have a table that has about 500000 rows.
Ok so there is a ITEM table and a TAG table, and an ITEM-TAG mapping table.
Tags and Items have an M:N relationship.
My Problem right now:
When i do a select item.id, item.name, item.owner, item.datecreated, from the tables, it is possible that I get multiple ITEMID because each item can have more than one tag mapped to it.
As such, in order to get a list of distinct items, my query is like this:
SELECT DISTINCT(item.id), name, owner, datecreated.
from item
LEFT JOIN itemtagmap ON (item.id = itemtagmap.itemid)
WHERE 1
(AND item.owner=???? AND itemtagmap.tagid IN (???,????) )
ORDER BY item.datecreated
The problem is now that because of the distinct, the query is darned slow.
Things speed up dramatically if i drop the distinct requirement. But that will totally screw up my pagination.
I have a multi column index in this order:
item.datecreated, item.owner, item.id,
It works absolutely fine as long as i don't have the DISTINCT.
The reason why i need to left join the ITEM to ITEMTAGMAP in the first place is because i have some code that allows me to append on additional WHERE itemtagmap.tagid = XXX clauses, so there has to be a join.
Would appreciate any help.... Many thanks...