Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 339
  • Last Modified:

Diagnose slow mysql query

Hi experts,

I have a query that is generated by a view in my Drupal website, so I don't have much control over the actual SQL. Problem I am having is that the query takes around 120 seconds to execute. If I reorder the query and run it I can do it in less than a second.

The original query is below

 
SELECT DISTINCT node.nid 
                AS 
                nid, 
                node.title 
                AS node_title, 
node_data_field_event_session_date_time.field_event_session_date_time_value AS 
node_data_field_event_session_date_time_field_event_session_date_time_value, 
location.latitude                                                           AS 
                gmap_lat, 
location.longitude                                                          AS 
                gmap_lon 
FROM   node node 
       LEFT JOIN content_field_event_session_date_time 
                 node_data_field_event_session_date_time 
         ON node.vid = node_data_field_event_session_date_time.vid 
       LEFT JOIN content_field_event_session_date_time 
                 node_data_field_event_session_date_time2 
         ON node.vid = node_data_field_event_session_date_time2.vid 
       LEFT JOIN content_field_event_session_date_time 
                 node_data_field_event_session_date_time3 
         ON node.vid = node_data_field_event_session_date_time3.vid 
       LEFT JOIN location_instance location_instance 
         ON node.vid = location_instance.vid 
       LEFT JOIN location location 
         ON location_instance.lid = location.lid 
       INNER JOIN domain_access da_admin 
         ON node.nid = da_admin.nid 
WHERE  ( ( da_admin.gid = 0 
           AND da_admin.realm = 'domain_site' ) 
          OR ( da_admin.gid = 6 
               AND da_admin.realm = 'domain_id' ) ) 
       AND ( ( ( node.status <> 0 ) 
               AND ( node.TYPE IN ( 'event' ) ) ) 
             AND 
       ( Date_format(Addtime( 
node_data_field_event_session_date_time.field_event_session_date_time_value2, 
Sec_to_time( 
node_data_field_event_session_date_time.field_event_session_date_time_offset)), 
'%Y-%m-%d') >= '2011-03-18' ) ) 
ORDER  BY 
node_data_field_event_session_date_time_field_event_session_date_time_value ASC

Open in new window


If I move the location and location_instance joins to the top of the from clause all my problems go away. I am wondering if anyone knows the reason for this and how to fix it without reordering the query? Let me know if you need to see the schema.

Regards,
Michael
0
mstrelan
Asked:
mstrelan
3 Solutions
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi mstrelan,

I suspect that there are a lot fewer rows in domain_access than in node.

Your query starts with a series of outer joins.  That means that the results of the join will have AT LEAST as many rows as the base table (node).  Remember that the result of a join is a derived table and that a derived table, by definition is not indexed.  So after the first outer join you write a derived table, then do a full table scan on it to join the next table.  If there are a lot of rows, this is time consuming.

An INNER join is a filter.  It reduces the number of rows in the resulting derived table by filtering out rows as part of the join operation.  By doing the inner join first, you generate fewer rows in the derived table this is used for the series of outer joins.


So in a nutshell, by doing the inner join first, you filter out rows before you do outer joins to them.  By doing the inner join last you outer join data that you'll never use and filter the results last.

I don't know of a way to force the order of the joins in MySQL and still use the indexes other than restructuring the SQL.


Good Luck,
Kent
0
 
mstrelanAuthor Commented:
Awesome, thanks for the explanation. Would index hints help? I saw this page http://dev.mysql.com/doc/refman/5.1/en/index-hints.html but wasn't sure how to use it.
0
 
JoeNuvoCommented:
I see you join with table content_field_event_session_date_time 3 times
But there is no uses of 2nd and 3rd called in the query
remove it might help on the speed

SELECT DISTINCT 
	node.nid AS nid, 
	node.title AS node_title, 
	node_data_field_event_session_date_time.field_event_session_date_time_value AS node_data_field_event_session_date_time_field_event_session_date_time_value,
	location.latitude AS gmap_lat, 
	location.longitude AS gmap_lon 
FROM   node node 
       INNER JOIN domain_access da_admin ON node.nid = da_admin.nid 
       LEFT JOIN content_field_event_session_date_time node_data_field_event_session_date_time 
         ON node.vid = node_data_field_event_session_date_time.vid 
       LEFT JOIN location_instance location_instance ON node.vid = location_instance.vid 
       LEFT JOIN location location ON location_instance.lid = location.lid 
WHERE
	(
	  ( da_admin.gid = 0 AND da_admin.realm = 'domain_site' ) 
	    OR
	  ( da_admin.gid = 6 AND da_admin.realm = 'domain_id' )
	)
	AND node.status <> 0
	AND node.TYPE IN ( 'event' )
	AND 
	Date_format(
	  Addtime(node_data_field_event_session_date_time.field_event_session_date_time_value2, 
	    Sec_to_time(node_data_field_event_session_date_time.field_event_session_date_time_offset)), 
	'%Y-%m-%d') >= '2011-03-18' 
ORDER  BY 
	node_data_field_event_session_date_time_field_event_session_date_time_value ASC

Open in new window

0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
mstrelanAuthor Commented:
Joe... in the interface there is an option to filter between 2 sets of dates. This query is the default with those dates not set.
0
 
JoeNuvoCommented:
I see,  first of all, try to see if my given code can do any better, in term of result speed.

and, please provide query when 2 sets of date is apply to filter.
I would like to see if I can do something with it.
0
 
LowfatspreadCommented:
do you potentially get multiple rows from location?

or would 1 suffice?

could you create a view as

select .....
        ,(select lat from ....) as gmap_lat
        ,(select long from ...) as gmap_long
from ....

and use that without the left joins to location....
0
 
mstrelanAuthor Commented:
@JoeNuvo

Yes, reducing to only one join for the dates speeds up the query. But filtering the date result set with a date range doesn't help.
0
 
JoeNuvoCommented:
still waiting for your version of query which all dates is set (which required 2nd and 3rd table to perform join)
0
 
mstrelanAuthor Commented:
@JoeNuvo
Here is with all the date filters

SELECT DISTINCT node.nid
                AS
                nid,
                node.title
                AS node_title,
node_data_field_event_session_date_time.field_event_session_date_time_value AS
node_data_field_event_session_date_time_field_event_session_date_time_value,
location.latitude                                                           AS
                gmap_lat,
location.longitude                                                          AS
                gmap_lon
FROM   node node
       LEFT JOIN location_instance location_instance
         ON node.vid = location_instance.vid
       LEFT JOIN location location
         ON location_instance.lid = location.lid
       LEFT JOIN content_field_event_session_date_time
                 node_data_field_event_session_date_time
         ON node.vid = node_data_field_event_session_date_time.vid
       LEFT JOIN content_field_event_session_date_time
                 node_data_field_event_session_date_time2
         ON node.vid = node_data_field_event_session_date_time2.vid
       LEFT JOIN content_field_event_session_date_time
                 node_data_field_event_session_date_time3
         ON node.vid = node_data_field_event_session_date_time3.vid
       INNER JOIN domain_access da_admin
         ON node.nid = da_admin.nid
WHERE  ( ( da_admin.gid = 0
           AND da_admin.realm = 'domain_site' )
          OR ( da_admin.gid = 6
               AND da_admin.realm = 'domain_id' ) )
       AND ( ( ( node.status <> 0 )
               AND ( node.TYPE IN ( 'event' ) ) )
             AND
       ( ( Date_format(Addtime(
node_data_field_event_session_date_time.field_event_session_date_time_value,
Sec_to_time(
node_data_field_event_session_date_time.field_event_session_date_time_offset)),
    '%Y-%m-%d') >= '2011-05-01'
AND Date_format(Addtime(
node_data_field_event_session_date_time.field_event_session_date_time_value,
Sec_to_time(
node_data_field_event_session_date_time.field_event_session_date_time_offset)),
'%Y-%m-%d') <= '2011-05-31' )
AND ( Date_format(Addtime(
node_data_field_event_session_date_time.field_event_session_date_time_value2,
Sec_to_time(
node_data_field_event_session_date_time.field_event_session_date_time_offset)),
'%Y-%m-%d') >= '2011-05-01'
AND
Date_format(
Addtime(
node_data_field_event_session_date_time.field_event_session_date_time_value2,
  Sec_to_time(
node_data_field_event_session_date_time.field_event_session_date_time_offset)),
'%Y-%m-%d') <= '2011-05-31' )
AND ( Date_format(Addtime(
node_data_field_event_session_date_time.field_event_session_date_time_value2,
Sec_to_time(
node_data_field_event_session_date_time.field_event_session_date_time_offset)),
'%Y-%m-%d') >= '2011-03-21' ) ) )
ORDER  BY
node_data_field_event_session_date_time_field_event_session_date_time_value ASC
LIMIT  0, 20 

Open in new window

0
 
mstrelanAuthor Commented:
@Everyone

I have solved the problem. After considering the comment from Kdo I added a where clause "location.latitude != 0" and was able to move this to the top via the Drupal view builder. This moved the location join to the top and has sped up my query considerably.

@JoeNuvo - feel free to keep working on improving this but for now I am happy with this solution
0
 
JoeNuvoCommented:
Your query with all dates filter, I still don't see any uses of 2nd and 3rd join of table content_field_event_session_date_time
so, I still recommend you to remove it from your query.

to combine with Kdo's comment, you shall move INNER JOIN of table domain_access to be 1st table to join. (as in my given query)
0
 
mstrelanAuthor Commented:
Included my comment in the solution as it explains how to fix it in Drupal
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now