Can I create MySQL View with SHOW keywords? - mysql

I need to create view like
create view GetProcessList
as
show processlist
Is it possible?

Is it possible?
No you can only create a view base on a SELECT statement , not a SHOW statement. But if your question is really how you can access the processlist as if it were a table....then this is already available in the information_schema meta-database:
MariaDB [information_schema]> SELECT * FROM information_schema.PROCESSLIST;
+----+------+-----------+--------------------+---------+------+----------------------+----------------------------------------------+---------+-------+-----------+----------+-------------+---------------+----------+
| ID | USER | HOST | DB | COMMAND | TIME | STATE | INFO | TIME_MS | STAGE | MAX_STAGE | PROGRESS | MEMORY_USED | EXAMINED_ROWS | QUERY_ID |
+----+------+-----------+--------------------+---------+------+----------------------+----------------------------------------------+---------+-------+-----------+----------+-------------+---------------+----------+
| 42 | root | localhost | information_schema | Query | 0 | Filling schema table | SELECT * FROM information_schema.PROCESSLIST | 0.561 | 0 | 0 | 0.000 | 83104 | 0 | 204 |
+----+------+-----------+--------------------+---------+------+----------------------+----------------------------------------------+---------+-------+-----------+----------+-------------+---------------+----------+
1 row in set (0.00 sec)

Related

mysql.user - list column (privileges) values as records

Need help to list all the privileges a user has (means, with "Y" on the column privileges) from the select * from mysql.user; result and have it as 1 column (as user_priv) together with host and user columns
query test user and its privileges details
Would like to turn above result to this one:
+------+------+-------------+
| user | host | user_priv |
+------+------+-------------+
| test | % | Create_priv |
+------+------+-------------+
| test | % | Reload_priv |
+------+------+-------------+
| test | % | ... |
You can get the data you are after by querying the information_schema.USER_PRIVILEGES table which derives it's data from mysql.user (documentation)
While you won't get the exact format you are after without a bit of string manipulation to separate user and host you will get more readable output for the privilege types.
e.g.
SELECT `GRANTEE` as 'USER#HOST', `PRIVILEGE_TYPE` as 'PRIVILEGE'
FROM `information_schema`.`USER_PRIVILEGES` WHERE `GRANTEE` LIKE '%paul%';
+--------------------+-------------------------+
| USER#HOST | PRIVILEGE |
+--------------------+-------------------------+
| 'paul'#'localhost' | SELECT |
| 'paul'#'localhost' | INSERT |
| 'paul'#'localhost' | UPDATE |
| 'paul'#'localhost' | DELETE |
| 'paul'#'localhost' | CREATE |
...
| 'paul'#'localhost' | DROP |
| 'paul'#'localhost' | RELOAD |
| 'paul'#'localhost' | EVENT |
| 'paul'#'localhost' | TRIGGER |
| 'paul'#'localhost' | CREATE TABLESPACE |
+--------------------+-------------------------+
If you want to know if the user can GRANT the privileges, add the column IS_GRANTABLE to your query.

MySQL performance_schema not working properly?

I have MySQL 5.6.19 community server running on my Ubuntu 14.04. Lately I have been trying to monitor MySQL server using performance_schema. Previously I used to do this by running SHOW FULL PROCESSLIST query every 5 second using a cronjob.
I configured performance_schema.setup_consumers table as follows.
mysql> use performance_schema;
mysql> select * from setup_consumers;
+--------------------------------+---------+
| NAME | ENABLED |
+--------------------------------+---------+
| events_stages_current | NO |
| events_stages_history | NO |
| events_stages_history_long | NO |
| events_statements_current | YES |
| events_statements_history | NO |
| events_statements_history_long | NO |
| events_waits_current | NO |
| events_waits_history | NO |
| events_waits_history_long | NO |
| global_instrumentation | YES |
| thread_instrumentation | YES |
| statements_digest | YES |
+--------------------------------+---------+
By default the events_statements_history is not enabled. I enabled it as follows.
mysql> update setup_consumers set enabled="YES" where name="events_statements_history";
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from setup_consumers;
+--------------------------------+---------+
| NAME | ENABLED |
+--------------------------------+---------+
| events_stages_current | NO |
| events_stages_history | NO |
| events_stages_history_long | NO |
| events_statements_current | YES |
| events_statements_history | YES |
| events_statements_history_long | NO |
| events_waits_current | NO |
| events_waits_history | NO |
| events_waits_history_long | NO |
| global_instrumentation | YES |
| thread_instrumentation | YES |
| statements_digest | YES |
+--------------------------------+---------+
Now enabled, the events_statements_history table should now be able to capture queries but somehow it is not. Following is the result.
mysql> select count(sql_text) from events_statements_history;
+-----------------+
| count(sql_text) |
+-----------------+
| 0 |
+-----------------+
I do not understand as to why it is not logging in any queries?
The performance schema records data when three conditions are met:
The running thread is instrumented
The instrument currently executed is enabled
The consumers for the data to record are enabled
Threads
By default, every thread (1) is instrumented.
To check if threads are instrumented:
select THREAD_ID, NAME, PROCESSLIST_ID, PROCESSLIST_USER, PROCESSLIST_HOST, INSTRUMENTED from threads;
To change which threads are instrumented by default, use table setup_actors.
Instruments
By default, instruments for statements (aka, queries), are enabled (2).
To see which queries are not instrumented:
select * from setup_instruments where name like "statement/%" and enabled='NO';
To instrument all queries:
update setup_instruments set enabled="YES" where name like "statement/%";
Consumers
By default, only a few consumers are instrumented (3).
To see which consumers are instrumented, or to change this, use select / update on table setup_consumers.
This part seems fine per the question, so check part (1) and (2).
As a side-note, if there is nothing inside the setup_% tables then you might need to enable the performance schema,
https://dev.mysql.com/doc/refman/5.5/en/performance-schema-quick-start.html

MySQL import taking a long time to finish/ never ends

My ~700mb database has been importing for 1hr 27min now, the actual disk activity stopped at around 15min but I just left it running to see if it would finish by itself.
The command I ran:
mysqldump DB1 | pv | mysql DB2
So it's pretty much a straight copy from 1 database to another, with DB2 starting as empty.
I can actually see that the data is already in DB2, but the command refuses to end!
So the question is... Should I let it continue to run? Or can I kill it? :/
Updated:
SHOW PROCESSLIST;
+------+--------------+---------------------+------------+---------+------+--------+------------------------------------------------------------------------------------------------------+
| Id | User | Host | db | Command | Time | State | Info |
+------+--------------+---------------------+------------+---------+------+--------+------------------------------------------------------------------------------------------------------+
| 2762 | user1 | localhost | DB2 | Query | 5298 | Locked | /*!50003 CREATE*/ /*!50020 DEFINER="user2"#"%"*/ /*!50003 FUNCTION "function1" |
| 2763 | user1 | localhost | DB1 | Sleep | 5298 | | NULL |
| 2770 | user2 | localhost | NULL | Query | 3633 | Locked | SELECT COUNT(*) FROM `INFORMATION_SCHEMA`.`ROUTINES` WHERE `ROUTINE_SCHEMA`='DB2' AND `ROUTIN |
| 2775 | user2 | localhost | NULL | Query | 381 | Locked | SELECT COUNT(*) FROM `INFORMATION_SCHEMA`.`ROUTINES` WHERE `ROUTINE_SCHEMA`='DB2' AND `ROUTIN |
| 2776 | user2 | <ipaddress>:<port> | NULL | Query | 0 | NULL | show processlist |
+------+--------------+---------------------+------------+---------+------+--------+------------------------------------------------------------------------------------------------------+
Some settings I have that are non-standard:
innodb_stats_on_metadata=0
innodb_flush_log_at_trx_commit=2

How to estimate SQL query timing?

I'm trying to get an rough (order-of-magnitude) estimate of how long time the following query could take:
mysql> EXPLAIN SELECT t1.col1, t1_col4 FROM t1 LEFT JOIN t2 ON t1.col1=t2.col1 WHERE col2=0 AND col3 IS NULL;
+----+-------------+--------------------+------+---------------+------------+---------+-----------------------------+---------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------------------+------+---------------+------------+---------+-----------------------------+---------+--------------------------+
| 1 | SIMPLE | t1 | ref | foobar | foobar | 4 | const | 9715129 | |
| 1 | SIMPLE | t2 | ref | col1 | col1 | 4 | db2.t1.col1 | 42318 | Using where; Using index |
+----+-------------+--------------------+------+---------------+------------+---------+-----------------------------+---------+--------------------------+
2 rows in set (0.00 sec)
mysql>
This can be done when using SHOW PROFILES syntax.
When you open a MySQL session, you could set the variable "profiling" to 1 or ON.
mysql> SET profiling = 1;
So all the statements sent to the server will be profiled and stored in a historical and shown later by typing the command:
mysql> SHOW PROFILES;
See, from MySQL manual:
mysql> SET profiling = 1;
Query OK, 0 rows affected (0.00 sec)
mysql> DROP TABLE IF EXISTS t1;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> CREATE TABLE T1 (id INT);
Query OK, 0 rows affected (0.01 sec)
mysql> SHOW PROFILES;
+----------+----------+--------------------------+
| Query_ID | Duration | Query |
+----------+----------+--------------------------+
| 0 | 0.000088 | SET PROFILING = 1 |
| 1 | 0.000136 | DROP TABLE IF EXISTS t1 |
| 2 | 0.011947 | CREATE TABLE t1 (id INT) |
+----------+----------+--------------------------+
3 rows in set (0.00 sec)
mysql> SHOW PROFILE;
+----------------------+----------+
| Status | Duration |
+----------------------+----------+
| checking permissions | 0.000040 |
| creating table | 0.000056 |
| After create | 0.011363 |
| query end | 0.000375 |
| freeing items | 0.000089 |
| logging slow query | 0.000019 |
| cleaning up | 0.000005 |
+----------------------+----------+
7 rows in set (0.00 sec)
mysql> SHOW PROFILE FOR QUERY 1;
+--------------------+----------+
| Status | Duration |
+--------------------+----------+
| query end | 0.000107 |
| freeing items | 0.000008 |
| logging slow query | 0.000015 |
| cleaning up | 0.000006 |
+--------------------+----------+
4 rows in set (0.00 sec)
mysql> SHOW PROFILE CPU FOR QUERY 2;
+----------------------+----------+----------+------------+
| Status | Duration | CPU_user | CPU_system |
+----------------------+----------+----------+------------+
| checking permissions | 0.000040 | 0.000038 | 0.000002 |
| creating table | 0.000056 | 0.000028 | 0.000028 |
| After create | 0.011363 | 0.000217 | 0.001571 |
| query end | 0.000375 | 0.000013 | 0.000028 |
| freeing items | 0.000089 | 0.000010 | 0.000014 |
| logging slow query | 0.000019 | 0.000009 | 0.000010 |
| cleaning up | 0.000005 | 0.000003 | 0.000002 |
+----------------------+----------+----------+------------+
References (updated at: 2014-09-04):
- SHOW PROFILE Syntax
- The INFORMATION_SCHEMA PROFILING Table
- How To Use MySQL Query Profiling (The Digital Ocean recently published a great article concerning this issue.)

why creating view from a left join view is not fast?

I created views on the tables
each of them had about 46299 rows
I created them successfully in phpMyadmin
after that I created a join of these two views
which itself is a view.
in mysql in a millisecond I got Query OK, 0 rows affected (0.01 sec)
but I couldn't reach my phpMyAdmin
so I decided to drop the view
in mysql it took about 20 minuets for dropping
again I tried my way and now I can't reach phpMyadmin
I don't know that it is under prcessing or not?!
how much I should wait? and why mysql show me fast query ok but phpmyAdmin is still processing if It is really under process
+-----+------+-----------------+-------+---------+------+--------------+--------
------------------------------------+
| Id | User | Host | db | Command | Time | State | Info
|
+-----+------+-----------------+-------+---------+------+--------------+--------
------------------------------------+
| 7 | root | localhost:38993 | week1 | Sleep | 2188 | | NULL
|
| 20 | root | localhost:39223 | week1 | Query | 0 | NULL | SHOW PR
OCESSLIST |
| 104 | root | localhost:39526 | NULL | Query | 800 | Sending data | SELECT
1 FROM `week1`.`alarm` LIMIT 100000 |
| 105 | root | localhost:39527 | week1 | Query | 800 | Sending data | SELECT
1 FROM `week1`.`alarm` LIMIT 100000 |
+-----+------+-----------------+-------+---------+------+--------------+--------
------------------------------------+
4 rows in set (0.00 sec)
mysql>
You can see if something have locked the table by issuing a SHOW PROCESSLIST command.