Solved

Why is this SQL (mySQL) stalling?

Posted on 2013-06-12
2
391 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 108

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now