Solved

mysql DELETE  "gets stuck"

Posted on 2014-12-01
6
215 Views
Last Modified: 2014-12-01
I've inherited the following "nested" DELETE query,
where the desire is to delete those records in the Purchase_order_details table that
are referred to in the Sales_order_details  table.

tblPOdetails                           tblSOdetails
po_detail_key    <-------------- po_detail_key
                                                 so_header_key   =   123456  (a specific value when handed to mySQL)

DELETE FROM tblPurchase_order_details       
                        WHERE po_detail_key IN
                              (SELECT po_detail_key FROM tblSales_order_details
                                    WHERE so_header_key='{$SOHID']}'
                              )

The server shows the mySQL state of this query to be          "Sending data"
and it's stuck there until i restart mySQL on the server.

I can unravel the "nested" query to be implemented using a PHP loop, but wondering if there's
a way it can work WITHIN mySQL?

p.s. it's INNODB with no constraints defined,  all other queries work fine.
the design, etc.  had been translated from an ASP/Microsoft implementation from 15+ yrs ago.
0
Comment
Question by:willsherwood
  • 3
  • 2
6 Comments
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
This '{$SOHID']}' is not a valid anything that I know of.  Please post the actual code in the 'code' format so we can see what you are actually doing.  You can replace any sensitive data but otherwise keep the code as it is found.  Like this...

DELETE FROM tblPurchase_order_details       
                        WHERE po_detail_key IN
                              (SELECT po_detail_key FROM tblSales_order_details
                                    WHERE so_header_key='{$SOHID']}'
                              ) 

Open in new window

0
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
Comment Utility
Is this your exact sql as you have a square bracket and a nested apostrophe

'{$SOHID']}'

DELETE tblPurchase_order_details FROM  tblPurchase_order_details 
	INNER JOIN tblSales_order_details 
		ON tblSales_order_details.po_detail_key = tblPurchase_order_details.po_detail_key
	WHERE so_header_key='{$SOHID}'

Open in new window

0
 

Author Comment

by:willsherwood
Comment Utility
sorry for the copy/paste typo.

the  '{$SOHID}'  is a PHP variable that expands to a literal/constant for the query

i'll try your INNER JOIN now !
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Closing Comment

by:willsherwood
Comment Utility
super, thanks for your help in translating to use  INNER JOIN - a great help!
0
 

Author Comment

by:willsherwood
Comment Utility
p.s.   just curious why something like this would "hang" mySQL for THIS database  when executed
 (other db were operational
during the period that this  query took hold of the the database)
0
 
LVL 58

Expert Comment

by:Gary
Comment Utility
Could be several things without knowing the db setup. But a join is better than a nested select which involves more work.
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

This guide whil teach how to setup live replication (database mirroring) on 2 servers for backup or other purposes. In our example situation we have this network schema (see atachment). We need to replicate EVERY executed SQL query on server 1 to…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

771 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

14 Experts available now in Live!

Get 1:1 Help Now