正在使用的数据库:5.6(我无法使用来自mysql 8的LAG函数(
我在 mysql 中有以下表结构
book_id | Version | Rating | Price
varchar(25) | Decimal(10,2)| int | Decimal(10,2)
我有一个网页,其中显示两个图表。
在第一个图表中,我将显示评级的书籍计数(评级仅从 1 到 4(,但仅显示最新版本。Query1
在第二个图表中,我将显示价格范围内的书籍数量,但仅显示最新版本的书籍数量。Query2
每次加载或刷新网页时,都会一个接一个地运行这两个查询。尽管数据保持不变,但对于同一查询,我有时会得到不同的结果。
我有以下两个几乎相同的查询
查询1
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
查询2
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
我的网页中有多个屏幕,并且有多个查询,但其中大多数都使用相同的逻辑。基本上,我将查询任何书籍的最新版本,因此我使用嵌套查询。
在某些情况下,当相同的查询在同一数据集上多次运行时,我得到的结果与预期不同。
我的查询是否正确? 使用变量是否会导致此问题? 由于多个查询是并行运行的(尽管在不同的数据库连接中(,变量的使用是否可疑?
您对变量的使用不正确。 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)
上索引时,这应该比使用变量的版本更快。