Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to select individual values using SHOW STATUS in MySQL?

Tags:

mysql

Is it possible to write a query that will return only the value of a variable returned by MySQL's SHOW STATUS? Ideally I want something like:

SELECT `Value` FROM (SHOW STATUS LIKE 'Com_delete')

or something similar.

Is this possible?

like image 677
petraus Avatar asked Dec 01 '10 16:12

petraus


People also ask

How can I see MySQL status?

We check the status with the systemctl status mysql command. We use the mysqladmin tool to check if MySQL server is running. The -u option specifies the user which pings the server. The -p option is a password for the user.

How do I select a variable in MySQL?

The syntax for assigning a value to a SQL variable within a SELECT query is @ var_name := value , where var_name is the variable name and value is a value that you're retrieving. The variable may be used in subsequent queries wherever an expression is allowed, such as in a WHERE clause or in an INSERT statement.

What is @@ in MySQL?

@@ is used for system variables. Using different suffix with @@ , you can get either session or global value of the system variable.

Is status a keyword in SQL?

You're correct that STATUS is a keyword, likely owing to SHOW STATUS . If renaming the column isn't much of a chore for you, changing the name is the best choice.


2 Answers

If you are using MySQL 5.1 or above, you should be able to get that data from INFORMATION_SCHEMA like this for global status:

select VARIABLE_VALUE 
from information_schema.GLOBAL_STATUS 
where VARIABLE_NAME = 'Com_delete';

Or if you want the session status instead:

select VARIABLE_VALUE 
from information_schema.SESSION_STATUS 
where VARIABLE_NAME = 'Com_delete';

The default for SHOW STATUS is SESSION status, so the latter query would work as a replacement for that.

like image 96
Ike Walker Avatar answered Oct 02 '22 00:10

Ike Walker


Complementing Ike Walker answer, INFORMATION_SCHEMA is deprecated on MySQL 5.7.6, as the Performance Schema tables are intended to replace the INFORMATION_SCHEMA tables.

So, by querying the INFORMATION_SCHEMA you may get an error like this:

The 'INFORMATION_SCHEMA.SESSION_STATUS' feature is disabled; see the documentation for 'show_compatibility_56'

According to this, you can set show_compatibility_56 to ON to enable backwards compatibility and keep using 5.6 syntax, or enable the PERFORMANCE_SCHEMA and query the new tables:

performance_schema.global_variables
performance_schema.session_variables

performance_schema.global_status
performance_schema.session_status
like image 39
SebaGra Avatar answered Oct 02 '22 00:10

SebaGra