我正在阅读这篇文章,似乎没有办法做这样的事情:
SELECT COUNT(*)
WHERE SHOW VARIABLES
WHERE Variable_name = 'innodb_ft_cache_size'
Run Code Online (Sandbox Code Playgroud)
我可以执行以下操作,但这在 MySQL 5.7 中不起作用(表中没有变量)
SELECT COUNT(*)
FROM information_schema.GLOBAL_VARIABLES
WHERE Variable_name = 'innodb_ft_cache_size' (always returns 0)
Run Code Online (Sandbox Code Playgroud)
我需要这样做的原因是在存储过程中检测他们是否有能力做全文。它需要在MySQLMySQL 和变体中工作,所以我不能简单地解析@@verison.
有没有另一种方法可以做到这一点?
这是一个works on 5.5,5.6, and 5.7用于检测是否存在全文功能的存储过程。这也应该适用于 mysql 变体
begin
set @supports_ft = 0;
set @information_schema_exists := (SELECT COUNT(SCHEMA_NAME) FROM information_schema.SCHEMATA WHERE SCHEMA_NAME = 'information_schema');
if @information_schema_exists > 0 then
set @information_schema_exists_global_vars_exists := (SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'information_schema' AND TABLE_NAME = 'GLOBAL_VARIABLES');
if @information_schema_exists_global_vars_exists > 0 then
set @supports_ft := (SELECT COUNT(*) FROM information_schema.GLOBAL_VARIABLES WHERE VARIABLE_NAME = 'innodb_ft_cache_size');
end if;
end if;
if @supports_ft = 0 then
set @performance_schema_exists := (SELECT COUNT(SCHEMA_NAME) FROM information_schema.SCHEMATA WHERE SCHEMA_NAME = 'performance_schema');
if @performance_schema_exists > 0 then
set @performance_schema_exists_global_vars_exists := (SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'performance_schema' AND TABLE_NAME = 'GLOBAL_VARIABLES');
if @performance_schema_exists_global_vars_exists > 0 then
set @supports_ft := (SELECT COUNT(*) FROM performance_schema.GLOBAL_VARIABLES WHERE VARIABLE_NAME = 'innodb_ft_cache_size');
end if;
end if;
end if;
Run Code Online (Sandbox Code Playgroud)
你可以运行
flush status;
show global variables like 'innodb_ft_cache_size';
show status like 'handler_read_rnd_next';
Run Code Online (Sandbox Code Playgroud)
如果它是 2(或增加 2),那么您就拥有了该变量。如果它是 1(或增加 1),则不需要。
这应该适用于每个版本。