I am sometimes curious just to do a quick command line query to count the number of tables in my database. Is that possible in MySQL? If so, what is the query?
You can try this SQL query:
USE <database_name>;
SELECT COUNT(*) FROM sys.tables;
In the command above, replace the placeholder with the actual database name.
Related
I would like to view the following columns in the query output in RDS MySQL. Could you help me to write sql query against the right system tables to view all the sql queries (from one specific db or all db ) executed by one specific database user or all users ?
The columns that I am trying to fetch are -
database user name
database name
tablename,
sql query id,
sql query text,
query start time
query end time
For example, I executed select count(*) and then trying to see the list of comamnds that I had executed, so, I tried to
query them using the system table "INFORMATION_SCHEMA.PROCESSLIST" but i coudldn't find the same. Please guide/correct me.
I connect the instance using "testuser" credentail and execute the following
create database testdb;
use testdb;
create table testdb.table01 as select * from testdb01.Persons;
select count(*) from testdb.table01;
SELECT * FROM INFORMATION_SCHEMA.PROCESSLIST WHERE USER='testuser';
228336 testuser 10.xx.xxx.xxx:50881 Sleep 14
228337 testuser 10.xx.xxx.xxx:50882 testdb Query 0 executing SELECT * FROM INFORMATION_SCHEMA.PROCESSLIST WHERE USER='testuser'
LIMIT 0, 1000
Thank you,
Kind regards,
sk
The INFORMATION_SCHEMA.PROCESSLIST is a monitor for all the corrently active queries on your DB, but if they are fast to execute you most likely never see them in that table
You could try to use the MySQL integrated logging procedure insted by logging all the queries within the mysql.general_log table and filter the user_host column
Refere to this question Log all queries in mysql
Keep in mind that this table can grow in size very quickly if not frequently cleared, also it can cause some performance issues due to resource consumption
I know that you can use
SELECT *
FROM table
WHERE id IN (ids)
In my case, I have 100,000 of ids.
I'm wondering if MySQL has a limit for IN clause. If anyone knows a more efficient way to do this, that would be great!
Thanks!
Just this week I had to kill -9 a MySQL 5.7 Server where one of the developers had run a query like you describe, with a list of hundreds of thousands of id's in an IN( ) predicate. It caused the thread running the query to hang, and it wouldn't even respond to a KILL command. I had to shut down the MySQL Server instance forcibly.
(Fortunately it was just a test server.)
So I recommend don't do that. I would recommend one of the following options:
Split your list of 100,000 ids into batches of at most 1,000, and run the query on each batch. Then use application code to merge the results.
Create a temporary table with an integer primary key.
CREATE TEMPORARY TABLE mylistofids (id INT PRIMARY KEY);
INSERT the 100,000 ids into it. Then run a JOIN query for example:
SELECT t.* FROM mytable AS t JOIN mylistofids USING (id)
Bill Karwin suggestions are good.
The number of values from IN clause is only limited by max_allowed_packet from my.ini
MariaDB creates a temporary table when the IN clause exceeds 1000 values.
Another problem with such number of IDs is the transfer of data from the PHP script (for example) to the MySQL server. It will be a very long query. You can also create a stored procedure with that select and just call it from you script. It will be more efficient in terms of passing data from your script to MySQL.
I swear, this is for educational purposes only...
The MySQL query looks like this:
SELECT *
FROM mytable
where name='value' AND addr='value'
UNION SELECT *
FROM mysql.user;
But the error I get is that there is no mysql.mytable table.
Is there anything I can do in my union or in either of those values to make my query valid? Other than explicitly stating the mytable database name in the code? Let's assume I can't change anything in the query before the 'value'.
It's not very clear what are you trying to select from information_schema.user. information_schema is the standard MySQL database, it's there by default. However there is no user table in this database.
To view a list of your databases, do this in your MySQL console:
show databases;
To view tables in a database:
use information_schema
show tables;
From what you are saying, it looks like you are using information_schema database by default. Also you are trying to select mytable in this database, this table doesn't seem to exist either. Can you run the commands above and show us the output?
Update: If you need to select from two different databases:
select * from mydatabase.mytable union select * from mysql.users
Either remove mydatabase if mytable is in your current database or provide a proper name of your database instead. mydatabase here is just an example.
However, as already been pointed out to you, you should either have identical columns in both tables, or specify identical columns to be selected from each table.
For functional and valid UNION clause, you must have the same columns in both queries. Missing columns in a select, highlight default NULL with aliases:
Select *, NULL as "column1" FROM table1 ...
I have 2 databases on a server:
database1
database2
I am trying to query a table in each database. However, when I try that, I get the following error:
Unknown table 'database2.client' in field list
Here is the code that I am using in a script running on database 1:
SELECT database2.client.id;
It returns the unknown table error. The odd thing is that if I query for the databases, it shows them both:
SHOW DATABASES
it returns the following:
information_schema
database1
database2
I'm not sure why the SHOW syntax confirms that both databases are there, but I'm not able to select data from the other table.
Try this:
SELECT id FROM database2.client;
I believe the correct syntax would be:
SELECT id FROM database2.client
Where id is the column, database2 is your database, and client is your table.
Can you select it if you first run use database2, and then SELECT client.id?
EDIT:
As many people have pointed out, you need to use the correct SELECT syntax.
USE database2;
SELECT id FROM client;
this seems stupid, but I really don't know this thing.
Today, I am in need to get number of properties in a mysql table (in phpmyadmin).
Can I get the count of the column names in phpmyadmin (mysql)?
For that You can write sql query in phpmyadmin.
SELECT count(*)
FROM information_schema.columns
WHERE table_name = '<table_name>'