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
Solved

how do I fix this MySQL query employing user variables?

Posted on 2011-02-26
4
428 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
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

840 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