Solved

Why is this SQL (mySQL) stalling?

Posted on 2013-06-12
2
395 Views
Last Modified: 2013-07-03
Hello experts,

I have a SQL statement I'm running in mySQL and it is stalling. I've narrowed it down to the NOT EXISTS subquery. Can you help me figure out what went wrong?

I'm trying to get data from piewarehouse filtered for pieIDs that were not refilled that day.

Thank you!

SELECT * FROM piewarehouse PW 
inner join toiletpaper TP on TP.tpid = PW.pieid 
where TP.rolltype in ('long', 'short', 'sheet')
and (not exists (select 1 from piewarehouse PW2 where PW2.pieID = PW.pieid and PW2.daterefilled = '2013-06-12')) 

Open in new window

0
Comment
Question by:EffinGood
2 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 39242399
Hi.

Are there really multiple records per pieID? If not, then this is simpler, right?

SELECT * /* I would spell out actual columns here versus using wildcard. */
FROM piewarehouse PW 
INNER JOIN toiletpaper TP ON TP.tpid = PW.pieid 
WHERE TP.rolltype in ('long', 'short', 'sheet')
AND PW.daterefilled <> '2013-06-12'
;

Open in new window


I would probably still add the other filter on date.
SELECT * /* I would spell out actual columns here versus using wildcard. */
FROM piewarehouse PW 
INNER JOIN toiletpaper TP ON TP.tpid = PW.pieid 
WHERE TP.rolltype in ('long', 'short', 'sheet')
AND PW.daterefilled <> '2013-06-12'
AND NOT EXISTS (
    SELECT 1
    FROM piewarehouse PW2
    WHERE PW2.pieID = PW.pieID
    AND PW2.daterefilled = '2013-06-12'
)
;

Open in new window


If it continues to give problems, open MySQL Workbench and run the query with EXPLAIN in front. One issue may be there is no index on pieID, daterefilled, or both.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39242787
If you have not already done so, you might want to read the article here:
http://www.experts-exchange.com/Database/MySQL/A_1250-3-Ways-to-Speed-Up-MySQL.html
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question