How would I optimize this query?

Discussion in 'Databases' started by bigrollerdave, Jan 18, 2009.

  1. #1
    Query
    SELECT filename, category, name, downloads FROM wallpapers WHERE pending='0' AND ((category LIKE '%".$search."%') OR (filename LIKE '%".$search."%') OR (tags LIKE '%".$search."%') OR (name LIKE '%".$search."%')) ORDER BY ordernum DESC
    PHP:
    Explain shows
    id 	select_type 	table 	type 	possible_keys 	key 	key_len 	ref 	rows 	Extra
    1 	SIMPLE 	wallpapers 	ALL 	pending 	NULL 	NULL 	NULL 	1158 	Using where; Using filesort
    PHP:
    I'm not sure what to index or how to optimize this query. Is there even anything that I could index to optimize it?
     
    bigrollerdave, Jan 18, 2009 IP
  2. mapAffiliation

    mapAffiliation Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Most probably the problem is in "filename LIKE '%SOMETHING%'".
    Even if you have an index on field "filename" it will work only for leftmost part of string, for string beginn. If you would have "filename LIKE 'SOMETHING%'" your index on "filename" will work, but in case "filename LIKE '%SOMETHING%'" it behaves like there is no index :( .
     
    mapAffiliation, Jan 18, 2009 IP
  3. chisara

    chisara Peon

    Messages:
    141
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Search the forums for people with your same problem using some sort of Full Text Indexing system.
    Refer to your unnamed database server supplier for documentation.
     
    chisara, Jan 19, 2009 IP