Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Why is this SQL (mySQL) stalling?

Posted on 2013-06-12
2
Medium Priority
?
399 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 2000 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 111

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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

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.
The viewer will learn how to dynamically set the form action using jQuery.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

688 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