Jump to content

how to optimize this query


mga_ka_php

Recommended Posts

how do you optimize this query?

SELECT SUBSTR(eventDetails, INSTR(eventDetails, '|#|Name') + 8, INSTR(eventDetails, '|#|StartDate') - INSTR(eventDetails, '|#|Name') -  eventTitle, SUBSTR(eventDetails, INSTR(eventDetails, '|#|City') + 8, INSTR(eventDetails, '|#|State') - INSTR(eventDetails, '|#|City') -  city, IF(UNIX_TIMESTAMP(SUBSTR(eventDetails, INSTR(eventDetails, '|#|StartDate') + 13, 10)) = 0, SUBSTR(eventDetails, INSTR(eventDetails, '|#|StartDate') + 13, 10), UNIX_TIMESTAMP(SUBSTR(eventDetails, INSTR(eventDetails, '|#|StartDate') + 13, 10))) eventStartDate, IF(UNIX_TIMESTAMP(SUBSTR(eventDetails, INSTR(eventDetails, '|#|EndDate') + 11, 10)) = 0, SUBSTR(eventDetails, INSTR(eventDetails, '|#|EndDate') + 11, 10), UNIX_TIMESTAMP(SUBSTR(eventDetails, INSTR(eventDetails, '|#|EndDate') + 11, 10))) eventEndDate, SUBSTR(eventDetails, INSTR(eventDetails, '|#|State') + 9, INSTR(eventDetails, '|#|Zip') - INSTR(eventDetails, '|#|State') - 9) eventState
FROM events 
WHERE eventDetails NOT LIKE '%Canceled:YES%' 
AND eventDetails NOT LIKE '%State:|#|%' 
AND eventDetails NOT LIKE '%Status:DELETED%' 
AND (eventDetails NOT LIKE '%PromoterPhone:|#|%' OR eventDetails NOT LIKE '%Link1:' OR eventDetails NOT LIKE '%UserNumber:0|#|%' OR eventDetails NOT LIKE '%UserNumber:|#|%')
AND SUBSTR(eventDetails, INSTR(eventDetails, '|#|State') + 9, INSTR(eventDetails, '|#|Zip') - INSTR(eventDetails, '|#|State') - 9) = 'RI' 
HAVING ((eventStartDate BETWEEN '1261980000' AND '1282712400') AND (eventEndDate BETWEEN '1261980000' AND '1282712400')) 
ORDER BY eventStartDate ASC LIMIT 0, 20;

Link to comment
Share on other sites

Redesign the database. Cancelled, State, Status, and so forth should probably be individual columns in the table, not embedded in a text field.

 

As it stands you need to do two things:

1) Change HAVING to AND - HAVING is optimized for GROUP BYs and will not filter the rows being selected, it filters the rows being returned.  A subtle difference, but it basically means that it will not be considered until all other conditions are met.

 

2) Make sure that eventStartDate and eventEndDate are individual indexes on the table.  They do not have to be unique indexes.  LIKE will never use an index, so you have to limit where it is looking. By changing the HAVING and letting the engine use an index, you should get better performance. You are most likely doing table scans right now.

 

I don't think there is anything else that can be done since all the other conditions are using LIKE.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.