Solved

Why is this SQL (mySQL) stalling?

Posted on 2013-06-12
2
397 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 60

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 110

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Oracle - BLOB Extract Line 2 15
history of pages 7 52
Select Query Fails in PHP but not in TERMINAL Mysql 9 31
Position image fpdf 4 18
This article discusses how to create an extensible mechanism for linked drop downs.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

734 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