MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 25th, 2012, 03:00 PM
lloydie-t lloydie-t is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2002
Location: UK
Posts: 563 lloydie-t User rank is Private First Class (20 - 50 Reputation Level)lloydie-t User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Day 23 h 54 m 22 sec
Reputation Power: 12
Slow 'NOT IN' query

I am suffering today with slow queries where there are a lot of records matching. I suspect it may be something to do with my 'NOT IN' operator, but not sure how to resolve.

Code:
SELECT SUM(amount) as amount FROM transactions WHERE account_id = 'L1095' 
AND (trans_date > '2012-08-23 13:37:00' OR (rent_run_id > '2012-08-23 13:37:00' AND trans_type = 5001)) 
AND rent_run_id < NOW() AND trans_type = 5001 AND amount > 0 
AND amount NOT IN (SELECT amount FROM transactions WHERE account_id = 'L1095' AND rent_run_id > NOW() AND trans_type = 5001)
__________________
Telephone System Super-Tech

Reply With Quote
  #2  
Old September 25th, 2012, 06:26 PM
cafelatte cafelatte is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Mar 2008
Posts: 1,923 cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 5 Days 16 h 21 m 8 sec
Reputation Power: 377
Code:
SELECT SUM(tx.amount) amount 
  FROM transactions tx
  LEFT
  JOIN transactions ty
    ON ty.account_id = tx.account_id
   AND ty.trans_type = tx.trans_type
   AND ty.rent_run_id > NOW()
   AND ty.amount = tx.amount
 WHERE tx.account_id = 'L1095' 
   AND (tx.trans_date > '2012-08-23 13:37:00' OR (rent_run_id > '2012-08-23 13:37:00')) 
   AND tx.rent_run_id < NOW() 
   AND tx.trans_type = 5001 
   AND tx.amount > 0
   AND ty.account_id IS NULL;


or something like that.

If it's still slow, and assuming the right results are returned, consider providing the EXPLAIN for the query, as well as a CREATE TABLE statement for the transactions table.

Reply With Quote
  #3  
Old September 25th, 2012, 07:02 PM
lloydie-t lloydie-t is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2002
Location: UK
Posts: 563 lloydie-t User rank is Private First Class (20 - 50 Reputation Level)lloydie-t User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Day 23 h 54 m 22 sec
Reputation Power: 12
Unfortunately that query was slightly slower
explain
Code:
'1', 'SIMPLE', 'tx', 'ALL', 'account_id', NULL, NULL, NULL, '27425', 'Using where'
'1', 'SIMPLE', 'ty', 'ALL', 'account_id', NULL, NULL, NULL, '27425', 'Using where; Not exists'


create table
Code:
delimiter $$

CREATE TABLE `transactions` (
  `idtrans` int(11) NOT NULL AUTO_INCREMENT,
  `account_id` varchar(45) NOT NULL,
  `tenant_id` varchar(45) DEFAULT NULL,
  `trans_id` int(11) NOT NULL,
  `job_id` int(11) DEFAULT NULL,
  `amount` decimal(8,2) NOT NULL DEFAULT '0.00',
  `description` text,
  `trans_date` datetime DEFAULT NULL,
  `trans_period` varchar(16) DEFAULT NULL,
  `rent_run_id` date DEFAULT NULL,
  `trans_type` varchar(45) DEFAULT NULL,
  `payment_ref` varchar(45) DEFAULT NULL,
  `payment_type` int(11) NOT NULL DEFAULT '6',
  `recon_date` date DEFAULT NULL,
  `recon_stat_no` varchar(45) DEFAULT NULL,
  `recon_id` int(11) NOT NULL DEFAULT '0',
  `recalled` smallint(6) NOT NULL DEFAULT '0',
  PRIMARY KEY (`idtrans`),
  FULLTEXT KEY `account_id` (`account_id`)
) ENGINE=MyISAM AUTO_INCREMENT=62353 DEFAULT CHARSET=latin1$$

Reply With Quote
  #4  
Old September 26th, 2012, 01:37 AM
cafelatte cafelatte is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Mar 2008
Posts: 1,923 cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 5 Days 16 h 21 m 8 sec
Reputation Power: 377
Do many of your queries use MATCH/AGAINST or CONTAINS on account_id?
Can account_id really be 45 characters in length?

Reply With Quote
  #5  
Old September 26th, 2012, 02:56 AM
lloydie-t lloydie-t is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2002
Location: UK
Posts: 563 lloydie-t User rank is Private First Class (20 - 50 Reputation Level)lloydie-t User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Day 23 h 54 m 22 sec
Reputation Power: 12
In the main account_id are 5-6 digits. I suppose I could simplify the tables by having a column for each of the mostly used account types, which sre integers. I have also just noticed a couple of columns should be intergers and not varchar.

Reply With Quote
  #6  
Old September 26th, 2012, 08:34 AM
Guelphdad's Avatar
Guelphdad Guelphdad is offline
Hockey face
Dev Shed God 7th Plane (8000 - 8499 posts)
 
Join Date: Nov 2001
Location: St. Catharines, Canada
Posts: 8,141 Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 3 Weeks 3 Days 20 h 34 m 13 sec
Reputation Power: 1315
The two date fields you are doing comparisons on don't have indexes so that would slow you down for sure, the query would have to do a table scan for each comparison.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > Slow 'NOT IN' query

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap