How to optimize this query

Discussion in 'Databases' started by fordP, Feb 23, 2008.

  1. #1
    This query is KILLING my website:
    SELECT pid, filepath, filename, url_prefix, filesize, pwidth, pheight, ctime, aid, keywords, votes, pic_rating, title, caption,hits,owner_id,owner_name from cpg1411_pictures WHERE ((aid='13'  ) )   ORDER BY title DESC  LIMIT 0 ,12
    Code (markup):
    Here is EXPLAIN:

    ID = 1
    select_type = SIMPLE
    table = cpg1411_pictures
    type = ref
    possible_keys = aid_approved, pic_aid
    key = aid_approved
    key_len = 4
    ref = const
    rows = 11507
    extra = using where, using filesort

    This table is used for storing information for retrieval


    Due to the large number of rows, where would you recommend adding the index?
     
    fordP, Feb 23, 2008 IP
  2. SoKickIt

    SoKickIt Active Member

    Messages:
    305
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    70
    #2
    aid + title
     
    SoKickIt, Feb 23, 2008 IP
  3. northwest

    northwest Peon

    Messages:
    277
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    add index to AID. It depends, if aid = '13' constitutes more than 25% (approx.) of the table data, then it is better not to add index.

    Add ordered index to TITLE OR on a regular basis re-insert the table by ordering by TITLE. i.e. create table temp as SELECT * from cpg1411_pictures. THEn replace the records with data in temp. this will save you some time in ordering the data because ORDER BY is resource hog...
     
    northwest, Feb 23, 2008 IP