When I execute a particular query from mysql prompt,for example
mysql>select version();
the output is as below along with the number of rows and time taken for query execution
+-------------+
| version() |
+-------------+
| 5.5.35 |
+-------------+
1 row in set (0.00 sec)
I need to execute mysql query from the commandline using "-e" option,i.e.when i execute
mysql -u xxx -p*** -e'select version()'
the following output gets displayed
+-------------+
| version() |
+-------------+
| 5.5.35 |
+-------------+
I need to show the output along with the rows and execution time, when query is executed from the commandline.I know that
select row_count();
and
select found_rows();
can be used to get the number of rows affected and number of rows in the table respectively based on the last query executed.
Is there any mysql options or any other way to get the desired output as in the case of former without adding additional commands or queries.
Related
My connection string for MySQL is:
"Server=localhost;User ID=root;Password=123;pooling=yes;charset=utf8;DataBase=.;"
My questions are :
What query should I write to get database names that exist?
What query should I write to get server version?
I have error because of my connection string ends with DataBase=.
What should I write instead of the dot?
SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA
SELECT VARIABLE_NAME, VARIABLE_VALUE FROM INFORMATION_SCHEMA.GLOBAL_VARIABLES WHERE VARIABLE_NAME = 'VERSION'
Use INFORMATION_SCHEMA as the database.
To get the list of databases, you can use SHOW DATABASES:
SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
+--------------------+
3 rows in set (0.01 sec)
To get the version number of your MySQL Server, you can use SELECT VERSION():
SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.1.45 |
+-----------+
1 row in set (0.01 sec)
As for the question about the connection string, you'd want to put a database name instead of the dot, such as Database=test.
show Databases;
Will return you all the registered databases.
And
show variables;
will return a bunch of name value pairs, one of which is the version number.
I have events every night in my MYSQL, and I don't really sure what is going on because it's still running in the morning even if I set it earlier than the other event.
The question is,
how can I check the history or the log of the ran events,
which one is locked at night or which one is ran on not ran?
Thank you
this line will help you:
SELECT * FROM INFORMATION_SCHEMA.events;
You can enable slow query log in MySQL server so as to log in the slow queries in a file or MySQL table. Follow theses steps:
Check if slow query log is enabled for your MySQL server or not. Execute these query on the MySQL server.
mysql> show global variables like "%slow_query_log%";
+---------------------+----------------------------------+
| Variable_name | Value |
+---------------------+----------------------------------+
| slow_query_log | OFF |
| slow_query_log_file | /var/lib/mysql/siddhant-slow.log |
+---------------------+----------------------------------+
2 rows in set (0.00 sec)
If slow query log is not enabled, enable it like this.(or you can enable it in my.cnf or my.ini MySQL configuration file)
mysql> set global slow_query_log="ON";
Query OK, 0 rows affected (0.01 sec)
Also check the long query running time i.e. the time taken by the query to be considered as a slow query. The queries taking more time than this value would be logged in the slow query log.
mysql> show global variables like "%long_query%";
+-----------------+-----------+
| Variable_name | Value |
+-----------------+-----------+
| long_query_time | 10.000000 |
+-----------------+-----------+
1 row in set (0.00 sec)
Set this value to your requirement as follows.(here i am setting it to two seconds)
mysql> set global long_query_time=2;
Query OK, 0 rows affected (0.00 sec)
Now the slow queries will be logged in the slow query log file path as earlier returned by the query. I requirements are such that I need to monitor MySQL in real-time, you can have a look at this commercial GUI MySQL- Monitoring Tool. It analyzes the slow query log in real time.
I'm performing the following MySQL query inside phpmyadmin. The found_rows() function is not returning any result.
SELECT SQL_CALC_FOUND_ROWS `name` , `ability`
FROM `magic`
WHERE `name` LIKE 'Black%'
GROUP BY `name`
ORDER BY `name`
LIMIT 0 , 10;
SELECT FOUND_ROWS();
I've tried running the Select queries separately, one after another, but I get unexpected results. The first query alone returns 13 results, which is the correct total. When I run the found_rows() query afterwards, 20 is returned.
How can I get Found_rows() to report properly?
Thanks,
skibulk
You can't, in PHPMyAdmin.
PHPMyAdmin uses the FOUND_ROWS() itself to tell you how many rows were found in your last query, and parses this itself to tell you 'x results found'.
Every time you do a simple SELECT, PHPMyAdmin adds a limit and executes this select, and the FOUND_ROWS afterwards to tell you how many results you would have found in without the LIMIT. This is how PHPMyAdmin offers paging (by default 30 elements), too.
Plus, you cannot call FOUND_ROWS() two times: the second time you call it, it will refer to the first time you called it. Since the first time returns 1 row, you get 1 as result.
Do note also: by default each query you enter in PHPMyAdmin opens a new connection to MySQL, executes the query, and closes the connection. Your next query will not know of the first query, unless you put them all together in the input-textarea, separated by semicolons.
If you want to use FOUND_ROWS(), use another mysql-client. The mysql commandline is fine:
mysql> SELECT SQL_CALC_FOUND_ROWS TABLE_NAME FROM `information_schema`.tables WHERE TABLE_NAME LIKE "COL%" LIMIT 3;
+---------------------------------------+
| TABLE_NAME |
+---------------------------------------+
| COLLATIONS |
| COLLATION_CHARACTER_SET_APPLICABILITY |
| COLUMNS |
+---------------------------------------+
3 rows in set (0.01 sec)
mysql> SELECT FOUND_ROWS( );
+---------------+
| FOUND_ROWS( ) |
+---------------+
| 5 |
+---------------+
1 row in set (0.00 sec)
I want to include the time it takes to run a query as part of the output. Is this possible?
For example, this query:
mysql> SELECT count(*) AS NumberOfUsers FROM mdl_user;
+---------------+
| NumberOfUsers |
+---------------+
| 5741 |
+---------------+
1 row in set (0.16 sec)
I want to run it so that the "0.16 sec" value appears in a second column. Something like:
mysql> SELECT
count(*) AS NumberOfUsers
, QUERY_TIME() AS TimeToRunQuery
FROM mdl_user;
+---------------+----------------+
| NumberOfUsers | TimeToRunQuery |
+---------------+----------------+
| 5741 | 0.16 sec |
+---------------+----------------+
1 row in set (0.16 sec)
Nope, sorry. If you're interested just for informational purposes, you can have your script simply time the query by recording the time when the query is sent and subtracting that from the time when the query completes.
In PHP, it'd look something like this:
$start_time = microtime();
execute_query();
$end_time = microtime() - $start_time; // execution time in microseconds
I am getting different results performing the exact same query using regular statements and prepared statements, and I think it's a type conversion bug.
mysql> show columns from server where field = "vlan";
+-------------+--------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------+------+-----+---------+-------+
| vlan | int(5) | YES | MUL | NULL | |
+-------------+--------+------+-----+---------+-------+
mysql> select hostname from server where `vlan` = '184.182' limit 1;
Empty set (0.00 sec)
mysql> prepare stupid from "select hostname from server where `vlan` = ? limit 1";
Query OK, 0 rows affected (0.00 sec)
Statement prepared
mysql> set #vlan = '184.182';
Query OK, 0 rows affected (0.00 sec)
mysql> execute stupid using #vlan;
+-------------------+
| hostname |
+-------------------+
| web20.servers.com |
+-------------------+
1 row in set (0.00 sec)
the real value of vlan is 184
it looks like the way mysql is handling type conversions is different for prepared statements and regular statements? does that make sense? how do i fix this?
The expected data type of prepared statement parameters is determined upon statement preparation, and type conversion to that data type takes place prior to statement execution.
In your example, an integer parameter is expected; therefore the provided string is cast to an integer (184) before the statement is executed, and the comparison between the integer column vlan and the parameter is successful for the matching record.
The "regular" statement, by contrast, compares the integer column with a string; therefore the arguments are compared as floating point numbers, and no record has a matching vlan.
To avoid this situation, ensure that the data type cannot be determined upon preparation (or that the determined data type does not lose any information) - for example:
prepare not_so_stupid from
"select hostname from server where `vlan` = CAST(? AS CHAR) limit 1"
;