同一查询在重复执行时返回不同的值

时间:2018-09-28 08:13:41

标签: mysql sql mysql-5.7 mysql-5.6 mysql5

正在使用的数据库:5.6(我无法使用来自mysql 8的LAG函数)

我在mysql中具有以下表结构

book_id     | Version      | Rating | Price 
varchar(25) | Decimal(10,2)| int    | Decimal(10,2)

我有一个网页,其中显示两个图表。

在第一张图表中,我将显示用于评级的图书数量(评级仅从1到4),但仅显示最新版本。 Query1

在第二张图表中,我将显示价格范围内的图书数量,但仅显示最新版本。 Query2

每次加载或刷新网页时,都会依次运行这两个查询。尽管数据保持不变,但有时对于同一查询我会得到不同的结果。

我有以下两个几乎相同的查询

QUERY1

SELECT  
       SUM(CASE WHEN rating=1 THEN 1 ELSE 0) AS rating1,
       SUM(CASE WHEN rating=2 THEN 1 ELSE 0) AS rating2,
       SUM(CASE WHEN rating=3 THEN 1 ELSE 0) AS rating3,
       SUM(CASE WHEN rating=4 THEN 1 ELSE 0) AS rating4
FROM (
       SELECT rating, row_number
       FROM (
              SELECT rating, 
                     @num:=IF(@group:=book_id, @num+1, 1) row_number,
                     @group:=book_id bi
              FROM book_database
              ORDER BY book_id, version DESC
             ) book
      HAVING book.row_number = 1
    ) book

QUERY2

SELECT  
       SUM(CASE WHEN price <= 1000 THEN 1 ELSE 0) AS cheap,
       SUM(CASE WHEN price >1000 THEN 1 ELSE 0) AS costly
FROM (
       SELECT price, row_number
       FROM (
              SELECT price, 
                     @num:=IF(@group:=book_id, @num+1, 1) row_number,
                     @group:=book_id bi
              FROM book_database
              ORDER BY book_id, version DESC
             ) book
      HAVING book.row_number = 1
    ) book

我的网页中有多个屏幕,并且有多个查询,但是其中大多数都使用相同的逻辑。基本上,我会查询任何书籍的最新版本,因此我使用嵌套查询。

在某些情况下,当同一查询在同一数据集上多次运行时,我得到的结果与预期不同。

我的查询正确吗? 使用变量会导致此问题吗? 由于多个查询是并行运行的(尽管在不同的数据库连接中),因此怀疑使用变量吗?

1 个答案:

答案 0 :(得分:0)

您对变量的使用不正确。 MySQL不保证SELECT子句中表达式的求值顺序,因此您应同时设置所有变量。

例如,第一个查询应该看起来像这样:

SELECT SUM( rating = 1 ) AS rating1,
       SUM( rating = 2 ) AS rating2,
       SUM( rating = 3 ) AS rating3,
       SUM( rating = 4 ) AS rating4
FROM (SELECT rating, book_id,
             (@rn := if(@b = book_id, @rn + 1,
                        if(@b := book_id, 1, 1)
                       )
             ) as rn
      FROM (SELECT rating, book_id, version
            FROM book_database
            ORDER BY book_id, version DESC
           ) book CROSS JOIN
           (SELECT @rn := 0, @b := -1) params
    ) book
WHERE rn = 1;

重要的部分是涉及变量的部分。我还简化了其他一些逻辑。

但是,您不需要变量:

SELECT SUM( rating = 1 ) AS rating1,
       SUM( rating = 2 ) AS rating2,
       SUM( rating = 3 ) AS rating3,
       SUM( rating = 4 ) AS rating4
FROM book_database b
WHERE b.version = (SELECT MAX(b2.version)
                   FROM book_database b2
                   WHERE b2.book_id = b.book_id
                  );

book_database(book_id, version)上有索引,这应该比使用变量的版本快。