MySQL是否在Having上使用索引?

发布于 2024-11-01 08:26:42 字数 1088 浏览 5 评论 0原文

我的查询的一部分如下所示:

HAVING date > '2011-04-13 04:28:03'

日期变量已建立索引,这对查询有任何影响吗?

EXPLAIN EXTENDED 似乎没有使用索引,但我不知道这是否只是因为我正在测试的数据库中有 4 行。

我的查询:

SELECT AVG(slen) FROM
(
SELECT date, COUNT(id) as slen
FROM table
WHERE product_id = 2830
GROUP BY id
HAVING date > '2011-04-13 04:28:02'
) as T

有几行具有不同的日期值。我想选择日期 > 的 ID 组'2011-04-13 04:28:02'。然后,我想要属于一个组的平均行数,没有日期条件

顺便说一句,查询本身还不起作用。

我的另一个担忧是日期是否 > '2011-04-13 04:28:02' 将使用我的日期列索引。

从此数据集中:

sid             datelast                product_id
782240551706    2011-04-13 00:51:52     2830
782240551706    2011-04-13 04:05:48     2830
782240551706    2011-04-13 04:28:03     2830
111111111111    2011-04-13 00:50:30     2830

期望的结果:

应选择 id 为 782240551706 的组,平均值应为 3。

以下查询产生期望的结果:

SELECT AVG(slen) FROM
(
SELECT date, COUNT(id) as slen
FROM table
WHERE product_id = 2830
GROUP BY id
HAVING **max(date)** > '2011-04-13 04:28:02'
) as T

A portion of my query looks like:

HAVING date > '2011-04-13 04:28:03'

The date variable is indexed, does this have any effect on the query?

EXPLAIN EXTENDED doesn't seem to be using the index, but I don't know if that's only because I have 4 rows in the database that I'm testing with.

My query:

SELECT AVG(slen) FROM
(
SELECT date, COUNT(id) as slen
FROM table
WHERE product_id = 2830
GROUP BY id
HAVING date > '2011-04-13 04:28:02'
) as T

There are a few rows that have different date values. I want to select groups of ID that have a date > '2011-04-13 04:28:02'. I then want the average number of rows that belong to a group, without the date condition.

The query as it is, does not work yet by the way.

My other concern was whether the date > '2011-04-13 04:28:02' would use my date column index.

From this dataset:

sid             datelast                product_id
782240551706    2011-04-13 00:51:52     2830
782240551706    2011-04-13 04:05:48     2830
782240551706    2011-04-13 04:28:03     2830
111111111111    2011-04-13 00:50:30     2830

Desired Result:

The group with id 782240551706 should be chosen, and the average should be 3.

The following query produces the desired result:

SELECT AVG(slen) FROM
(
SELECT date, COUNT(id) as slen
FROM table
WHERE product_id = 2830
GROUP BY id
HAVING **max(date)** > '2011-04-13 04:28:02'
) as T

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北音执念 2024-11-08 08:26:42

HAVING 与 GROUP BY 结合使用,因此它是一个派生表。我不明白如何使用索引。

HAVING is used in conjunction with a GROUP BY, so it's a derived table. I don't see how there would be an index to use.

放手` 2024-11-08 08:26:42

例如:

SELECT 
    column1, 
    count(column2) as count 
FROM table 
GROUP BY column1 
HAVING count > 1

计数是根据您的查询计算的。它没有在您的案例中编入索引。您可以将having 子句更改为where 子句。

For example:

SELECT 
    column1, 
    count(column2) as count 
FROM table 
GROUP BY column1 
HAVING count > 1

The count is calculated by your query. It's not indexed in your case. You can change the having clause to a where clause.

相守太难 2024-11-08 08:26:42

该查询没有意义,因为“date”列位于 GROUP BY 中,但既不是聚合也不是 GROUP BY 子句。

任何普通的 SQL 数据库都会拒绝它;如果在严格模式下mysql会拒绝它。

在这种情况下,行为基本上是未定义的。不要这样做。

我很确定您想要 WHERE 来过滤行,而不是 HAVING。如果您不想在列列表中指定日期,请尝试使用 min(date) 或 max(date) 代替。

不,HAVING 不会使用索引,因为它需要执行 GROUP BY 来查找 HAVING 匹配的组。通常最好使用 WHERE,并且只能在聚合上使用 HAVING(例如 HAVING COUNT(*) > 1)

That query does not make sense, as the column "date" is in a GROUP BY, but is neither an aggregate nor GROUP BY clause.

Any normal SQL database would reject it; mysql would reject it if in strict mode.

The behaviour is basically undefined in this case. Don't do it.

I'm pretty sure you want WHERE to filter your rows, not HAVING. And you don't want to specify date in the column list, try min(date) or max(date) instead.

And no, HAVING won't use an index because it needs to perform the GROUP BY to find the groups which HAVING matches. Normally it is better to use WHERE, and you'd only use HAVING on an aggregate (e.g. HAVING COUNT(*) > 1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文