Solved

Why is this SQL (mySQL) stalling?

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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 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…

624 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