Advertisement

09.22.2008 at 10:04AM PDT, ID: 23752107
[x]
Attachment Details

MySQL slow query

[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.8
Zone:

MySQL Server

Tags:

MySQL, any browser type

phpMyAdmin 2.10.0.2 / MySQL 5.0.45

I have written an inventory program. My database contains these relevant tables: Inventory and Client_Inv. The inventory table contains all items. The Client_Inv contains the information necessary to determine which items belong to which clients - as well as the full item name, location, and limits. The inventory table contains a row for each item - so an item with a quantity of 500 will have 500 entries into the table to show when items were added, removed, or on order.

My query is designed to show all relevant information (location, item name, quantity on hand, and what is currently on order)  to the end user on a PHP page.

On the PHP page there is a drop down menu where the user selects which client's inventory to view and it then runs the query attached.

The query currently takes 13 seconds to run and I'm trying to determine how to possibly cut that down. On clients with inventory for each item, the time is considerably lower - however, if a client has an item associated to them but no available inventory - the query slows down. I've tried just running the first part of the query and it is incredibly slow without the UNION statement.

I have attached the explain details. Any information or direction on how to possibly beat my current 13 second execution time would be greatly appreciated. Thanks!
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
(SELECT Client_Inv.ItemID, Client_Inv.ItemName, Client_Inv.Location, count(*) AS count, 
SUM(IF(Client_Inv.ItemID = Inventory.ItemID AND OnOrder= '1', 1, 0)) AS Acount FROM Client_Inv
 LEFT JOIN Inventory ON Client_Inv.ItemID = Inventory.ItemID WHERE Client_Inv.Client_ID = '$q' AND 
Client_Inv.Discod = '0000-00-00' AND Inventory.RemDate = '0000-00-00' AND Inventory.Discod = '0' 
GROUP BY ItemID)
 
 UNION 
 
(SELECT Client_Inv.ItemID, Client_Inv.ItemName, Client_Inv.Location, count(NULL) AS num,
 SUM(IF(Client_Inv.ItemID = Inventory.ItemID AND OnOrder= '0', 0, 0)) AS numb FROM Client_Inv, Inventory WHERE
 Client_Inv.Client_ID = '$q' AND Client_Inv.Discod = '0000-00-00' AND Client_Inv.ItemID NOT IN 
(SELECT DISTINCT ItemID FROM Inventory WHERE Inventory.RemDate = '0000-00-00') GROUP BY ItemID) Order By 
ItemName
Attachments:
 
 
 
Expert Comment by mfsamuel:

All comments and solutions are available to Premium Service Members only. Start your 7-day free trial to view the solution to this question.

Already a member? Login to view this solution.

 
 
Author Comment by westpress:

All comments and solutions are available to Premium Service Members only. Start your 7-day free trial to view the solution to this question.

Already a member? Login to view this solution.

 
 
Expert Comment by mfsamuel:

All comments and solutions are available to Premium Service Members only. Start your 7-day free trial to view the solution to this question.

Already a member? Login to view this solution.

 
 
Author Comment by westpress:

All comments and solutions are available to Premium Service Members only. Start your 7-day free trial to view the solution to this question.

Already a member? Login to view this solution.

 
 
Expert Comment by mfsamuel:

All comments and solutions are available to Premium Service Members only. Start your 7-day free trial to view the solution to this question.

Already a member? Login to view this solution.

 
 
Author Comment by westpress:

All comments and solutions are available to Premium Service Members only. Start your 7-day free trial to view the solution to this question.

Already a member? Login to view this solution.

 
 
Expert Comment by mfsamuel:

All comments and solutions are available to Premium Service Members only. Start your 7-day free trial to view the solution to this question.

Already a member? Login to view this solution.

 
 
Expert Comment by mfsamuel:

All comments and solutions are available to Premium Service Members only. Start your 7-day free trial to view the solution to this question.

Already a member? Login to view this solution.

 
 
Author Comment by westpress:

All comments and solutions are available to Premium Service Members only. Start your 7-day free trial to view the solution to this question.

Already a member? Login to view this solution.

 
 
Expert Comment by mfsamuel:

All comments and solutions are available to Premium Service Members only. Start your 7-day free trial to view the solution to this question.

Already a member? Login to view this solution.

 
 
Expert Comment by mfsamuel:

All comments and solutions are available to Premium Service Members only. Start your 7-day free trial to view the solution to this question.

Already a member? Login to view this solution.

 
 
Author Comment by westpress:

All comments and solutions are available to Premium Service Members only. Start your 7-day free trial to view the solution to this question.

Already a member? Login to view this solution.

 
 
Author Comment by westpress:

All comments and solutions are available to Premium Service Members only. Start your 7-day free trial to view the solution to this question.

Already a member? Login to view this solution.

 
 
Author Comment by westpress:

All comments and solutions are available to Premium Service Members only. Start your 7-day free trial to view the solution to this question.

Already a member? Login to view this solution.

 
 
Accepted Solution by mfsamuel:

All comments and solutions are available to Premium Service Members only. Start your 7-day free trial to view the solution to this question.

Already a member? Login to view this solution.

 
 
Author Comment by westpress:

All comments and solutions are available to Premium Service Members only. Start your 7-day free trial to view the solution to this question.

Already a member? Login to view this solution.

 
 
Expert Comment by mfsamuel:

All comments and solutions are available to Premium Service Members only. Start your 7-day free trial to view the solution to this question.

Already a member? Login to view this solution.

 
 
20081119-EE-VQP-46 / EE_QW_2_20070628