Jump to content

Recommended Posts

Hey guys... I've tried google and reference material, but without knowing what direction to look I'm screwed. I have a query that simply hangs until it times out. I'm sure I'm doing something stupidly wrong, but I have no idea what it is. Could someone at least point me in the right direction? Query below:

 

select

month(DateTime) as m, year(DateTime) as y,

(select count(*) from Transaction inner join Device on Device.MAC_Address = Transaction.MAC_Address where DiagCode in ('52','53','54') and month(DateTime) = m and year(DateTime) = y) as fanCount,

(select count(*) from Transaction inner join Device on Device.MAC_Address = Transaction.MAC_Address where DiagCode in ('56','57','58') and month(DateTime) = m and year(DateTime) = y) as humCount,

(select count(*) from Transaction inner join Device on Device.MAC_Address = Transaction.MAC_Address where DiagCode = '59' and month(DateTime) = m and year(DateTime) = y) as scent1Count,

(select count(*) from Transaction inner join Device on Device.MAC_Address = Transaction.MAC_Address where DiagCode = '60' and month(DateTime) = m and year(DateTime) = y) as scent2Count,

(select count(*) from Transaction inner join Device on Device.MAC_Address = Transaction.MAC_Address where DiagCode = '61' and month(DateTime) = m and year(DateTime) = y) as scent3Count,

(select count(*) from Transaction inner join Device on Device.MAC_Address = Transaction.MAC_Address where DiagCode = '62' and month(DateTime) = m and year(DateTime) = y) as scent4Count,

(select count(*) from Transaction inner join Device on Device.MAC_Address = Transaction.MAC_Address where DiagCode = '63' and month(DateTime) = m and year(DateTime) = y) as scent5Count

from Transaction group by month(DateTime), year(DateTime);

Link to comment
https://forums.phpfreaks.com/topic/244334-query-hangs-help/
Share on other sites

'1', 'PRIMARY', 'Transaction', 'ALL', NULL, NULL, NULL, NULL, '74370', 'Using temporary; Using filesort'

'8', 'DEPENDENT SUBQUERY', 'Device', 'index', 'idxMAC', 'idxMAC', '19', NULL, '17', 'Using index'

'8', 'DEPENDENT SUBQUERY', 'Transaction', 'ref', 'idxDiagCodeMAC', 'idxDiagCodeMAC', '21', 'hotelairspa.Device.MAC_Address,const', '112', 'Using where'

'7', 'DEPENDENT SUBQUERY', 'Device', 'index', 'idxMAC', 'idxMAC', '19', NULL, '17', 'Using index'

'7', 'DEPENDENT SUBQUERY', 'Transaction', 'ref', 'idxDiagCodeMAC', 'idxDiagCodeMAC', '21', 'hotelairspa.Device.MAC_Address,const', '112', 'Using where'

'6', 'DEPENDENT SUBQUERY', 'Device', 'index', 'idxMAC', 'idxMAC', '19', NULL, '17', 'Using index'

'6', 'DEPENDENT SUBQUERY', 'Transaction', 'ref', 'idxDiagCodeMAC', 'idxDiagCodeMAC', '21', 'hotelairspa.Device.MAC_Address,const', '112', 'Using where'

'5', 'DEPENDENT SUBQUERY', 'Device', 'index', 'idxMAC', 'idxMAC', '19', NULL, '17', 'Using index'

'5', 'DEPENDENT SUBQUERY', 'Transaction', 'ref', 'idxDiagCodeMAC', 'idxDiagCodeMAC', '21', 'hotelairspa.Device.MAC_Address,const', '112', 'Using where'

'4', 'DEPENDENT SUBQUERY', 'Device', 'index', 'idxMAC', 'idxMAC', '19', NULL, '17', 'Using index'

'4', 'DEPENDENT SUBQUERY', 'Transaction', 'ref', 'idxDiagCodeMAC', 'idxDiagCodeMAC', '21', 'hotelairspa.Device.MAC_Address,const', '112', 'Using where'

'3', 'DEPENDENT SUBQUERY', 'Device', 'index', 'idxMAC', 'idxMAC', '19', NULL, '17', 'Using index'

'3', 'DEPENDENT SUBQUERY', 'Transaction', 'ref', 'idxDiagCodeMAC', 'idxDiagCodeMAC', '19', 'hotelairspa.Device.MAC_Address', '524', 'Using where'

'2', 'DEPENDENT SUBQUERY', 'Device', 'index', 'idxMAC', 'idxMAC', '19', NULL, '17', 'Using index'

'2', 'DEPENDENT SUBQUERY', 'Transaction', 'ref', 'idxDiagCodeMAC', 'idxDiagCodeMAC', '19', 'hotelairspa.Device.MAC_Address', '524', 'Using where'

 

 

I was trying to get the explain output for you but it was still hung. I had to restart mysql to be able to run explain. Sorry man.

Link to comment
https://forums.phpfreaks.com/topic/244334-query-hangs-help/#findComment-1255021
Share on other sites

I think that, instead of doing so many sub-queries, I would do something along these lines:

 

select month(DateTime) as m, year(DateTime) as y,
SUM(IF DiagCode in ('52','53','54') THEN 1 ELSE 0 END) AS fanCount,
SUM(IF DiagCode in ('56','57','58') THEN 1 ELSE 0 END) as humCount,
...
from Transaction join Device on Device.MAC_Address = Transaction.MAC_Address 
group by month(DateTime), year(DateTime);

 

Be aware that DateTime and Transaction are reserved words in mySql and will likely give you problems trying to use them in queries. I would consider renaming these columns/tables.

 

Link to comment
https://forums.phpfreaks.com/topic/244334-query-hangs-help/#findComment-1255032
Share on other sites

Oops, I started with a CASE statement, the syntax for IF would be:

 

select month(DateTime) as m, year(DateTime) as y,
SUM(IF DiagCode in ('52','53','54'), 1, 0) AS fanCount,
SUM(IF DiagCode in ('56','57','58'), 1, 0) as humCount,
...
from Transaction join Device on Device.MAC_Address = Transaction.MAC_Address 
group by month(DateTime), year(DateTime);

 

If you start having problems with the column names, you can use back-ticks to quote them:

 

SELECT `DateTime` ...

Link to comment
https://forums.phpfreaks.com/topic/244334-query-hangs-help/#findComment-1255055
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.