mysql - Searching from two columns is extremely slow -


Resolved : Andy Lester pointed me towards the right direction, this poor index definitions Was due to

is a part of a query which is causing me a lot of trouble:

  Select from item i.id as i tag_ rel is tr2 ON (tr2) Connect). Item = i.id) T2 On (T.id = tr2.tag) as the left join tag WHERE T2.name like 'chocolate' group i.id   

Items item table

tag_rel are the relationships between the object and the tag

Tags has the name tag

So the query above is OK, as it should: It gives returns to the items tagged as chocolate

But whatever is doing here, the query Slows down (10 seconds +) so I think That I am doing something wrong: - /

  Select from item i.id as I add tag 2 to tr2 (tr2.item = i.id) in the form of LEFT tags In T2 (t2.id = tr2.tag and t2.sec_corrected = 0) Add tags AS T3 ON (T.sec_corrected = tr2.tag and t3.sec_corrected> 0) WHERE (T2.name such as' choclate 'Or' toc '), such as' choclate'), group I.id   

sec_corrected "tag" table containing the ID of the second tag, i.e.:

  tag table structure: id 1 name choclate sec_corrected 2 id 2 name chocolate sec_corrected 0   

If the sub for "chocolate" or "chocolate" Ogkrta searches, so "chocolate" as I get "to be tagged as" chocolate "item" has been corrected as "chocolate" as "chocolate".

I have also tried with only one left pair, but it is still slow:

  Choose from item as i.id item tag 2 on tr2 (tr2. = I.id) as the left join tag T2 on (T.id = tr2.tag or t2.sec_corrected = tr2.tag) where (T2 is a group called 'choclate' ) i.id   

Any ideas?

Thank you very much!

Without explaining it, there is little effort and error, but usually a simple "or" section y Where with the use can block the index use. As is the overflow, use "=" when no pattern is given.

Simple solution divided into two inner joins with a union is ugly, but often useful.

  Select from item i.id as the item I insert INNER as Tag 2 on TR2 (tr2.item = i.id). In the form of INNER tag (t2.id = tr2.tag and t2.sec_corrected = 0) where (t2.name = 'choclate') select the union item from i.id as i tag as INNER Add 2 to tr2 (tr2.item = i.id) as INNER tag on T3 (t3.sec_corrected = tr2.tag and t3.sec_corrected> 0) WHERE (t3.name = 'choclate' )   

which can help a lot.

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -