[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

how do I fix this MySQL query employing user variables?

Posted on 2011-02-26
4
Medium Priority
?
434 Views
Last Modified: 2012-05-11
I have a table: products (product_id, shop_id, date)

For each shop_id, the following query retrieves the 10 rows with the most recent dates

set @num := 0, @shop_id := 0;

select product_id, shop_id, date
from (
   select product_id, shop_id, date,
      @num := if(@shop_id = shop_id, @num + 1, 1) as row_number,
      @shop_id := shop_id as dummy
  from products
  order by shop_id, date DESC
) as x where x.row_number <= 10;

Open in new window


In addition to the date and shop_id, I would also like to select the product_id for each row. However while the above query executes in about 0.35 secs on 80k of dummy data, and adding product_id to the subquery:

set @num := 0, @shop_id := NULL;# MySQL returned an empty result set (i.e. zero rows).
# MySQL returned an empty result set (i.e. zero rows).
# MySQL returned an empty result set (i.e. zero rows).
# MySQL returned an empty result set (i.e. zero rows).

select shop_id, date
from (
   select product_id, shop_id, date,
      @num := if(@shop_id = shop_id, @num + 1, 1) as row_number,
      @shop_id := shop_id as dummy
  from products
  order by shop_id, date DESC
) as x where row_number <= 10;

Open in new window


works just as well, adding product_id to the main query:

set @num := 0, @shop_id := NULL;

select product_id, shop_id, date
from (
   select product_id, shop_id, date,
      @num := if(@shop_id = shop_id, @num + 1, 1) as row_number,
      @shop_id := shop_id as dummy
  from products
  order by shop_id, date DESC
) as x where row_number <= 10;

Open in new window


causes MySQL to hang indefinitely. I am wondering what I am doing wrong.
0
Comment
Question by:bitt3n
[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
  • 3
4 Comments
 
LVL 39

Expert Comment

by:Aaron Tomosky
ID: 34988911
It's having to calculate all results and then just give you the top 10. Instead just add limit 10 to the en of the query and get rid of the row numbers.
0
 

Author Comment

by:bitt3n
ID: 34988924
I'm getting the top 10 results for each shop_id, not limiting the query to 10 results.
0
 

Accepted Solution

by:
bitt3n earned 0 total points
ID: 34989391
I am closing and reposting this question because I believe it can be stated more clearly that I in my original post.
0
 

Author Closing Comment

by:bitt3n
ID: 35025216
closing to rephrase
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In this article, we’ll look at how to deploy ProxySQL.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

650 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