[英]Can you create an index to determine if a text column is null in mysql?
I have a very large (> 100 million rows) table and a query that I need to write needs to check if a column of type TEXT
is null. 我有一个非常大(> 1亿行)的表和我需要编写的查询需要检查TEXT
类型的列是否为空。 I don't care about the actual text, only if the column value is null or not. 我不关心实际文本,只有当列值为null时才关注。
I'm trying to write an index to make that query efficient, and I want the query to be satisfied by only looking at the index eg such that the explain output of that query shows Using index
instead of Using index condition
. 我正在尝试编写索引以使该查询有效,并且我希望仅通过查看索引来满足查询,例如,使得该查询的解释输出显示Using index
而不是Using index condition
。
To be concrete, the table I'm dealing with has a structure like this: 具体来说,我正在处理的表格有这样的结构:
CREATE TABLE foo (
id INT NOT NULL AUTO_INCREMENT,
overridden TEXT,
rowState VARCHAR(32) NOT NULL,
active TINYINT NOT NULL DEFAULT 0,
PRIMARY KEY (id),
) ENGINE=InnoDB
And the query I want is this: 我想要的查询是这样的:
SELECT
IF(overridden IS NULL, "overridden", rowState) AS state,
COUNT(*)
FROM foo
WHERE active=1
GROUP BY state
The overridden
column is a description of why the column is overridden but, as I mentioned above, I don't care about its content, I only want to use it as a boolean flag. overridden
列是对列重写的原因的描述,但正如我上面提到的,我不关心它的内容,我只想将它用作布尔标志。
I've tried creating an index on (active, rowState, overridden(1))
but the explain output still shows Using index condition
. 我已尝试在(active, rowState, overridden(1))
上创建索引(active, rowState, overridden(1))
但解释输出仍显示Using index condition
。
Is there something better that I could do? 我能做些什么更好的事情吗?
I suspect this would be a good application for a "prefix" index. 我怀疑这对于“前缀”索引来说是一个很好的应用程序。 (Most attempts at using such are a fiasco.) (大多数尝试使用这种都是惨败。)
The optimal order, I think, would be: 我认为最佳订单是:
INDEX(active, -- handle the entire WHERE first
overridden(1), -- IS NULL possibly works well
rowState) -- to make index "covering"
Using index condition
means that the selection of the row(s) is handled in the Engine, not the "Handler". Using index condition
意味着在引擎中处理行的选择,而不是“处理程序”。
EXPLAIN FORMAT=JSON SELECT ...
may provide more insight into what is going on. EXPLAIN FORMAT=JSON SELECT ...
可以提供有关正在发生的事情的更多信息。 So might the "Optimizer trace". 那么“优化器跟踪”可能也是如此。
Or... 要么...
If the MySQL version is new enough, create a "generated column" that persists with the computed value of overridden IS NULL
. 如果MySQL版本足够新,则创建一个“生成列”,该列保持计算的overridden IS NULL
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.