How to make query to information_schema.tables to get list of available tables which is exist in one DB but not exist in another one, something like diff but more suitable. I just need sql query.
So i have Db's like A,B,C,D and all these DB's should has the same tables, how I can check it ?
try
select *
from INFORMATION_SCHEMA.tables
group by table_name
having count(table_schema) < 4
if you have 4 DB's. If more you have to adjust the having clause.
This query give you all unique tables in all databases.
SELECT *,count(TABLE_NAME)
FROM
`TABLES` group by table_name
having count(TABLE_NAME)=1
And if you want repeated table names then use this
SELECT *,count(TABLE_NAME)
FROM
`TABLES` group by table_name
having count(TABLE_NAME)>1
Related
I am trying to optimize a few stored procedures in a MySQL database. A few columns have more than one index. These columns are used in joins and WHERE clauses.
Which do I keep and which do I remove to improve query performance, and why?
select table_name,index_name,seq_in_index,column_name,non_unique,index_type,comment from information_schema.statistics
where table_name in('suspicious_activity_report','customer_comment_table','employement_status','Country_Master','Country_Master')
order by table_name,column_name
Your query's output shows tables from all the databases on your system. You need to show which database each table is in, like this:
select table_schema, table_name, index_name, seq_in_index,
column_name, non_unique, index_type, comment
from information_schema.statistics
where table_name in( 'suspicious_activity_report',
'customer_comment_table',
'employement_status',
'Country_Master')
order by table_schema, table_name, seq_in_index;
You can also say WHERE ... table_schema = DATABASE() if you want to show information from only the current database.
This will remove the apparently duplicated indexes from your output.
Multi-column indexes are often added to tables to handle specific query patterns. Your output shows only one multicolumn index: country_master(Country_ID, Client_ID). This index is suitable for these patterns, among a few others.
WHERE Country_ID = <constant> AND Client_ID = <constant>
WHERE Country_ID = <constant> AND Client_ID BETWEEN <constant> AND <constant>.
Like #P.Salmon said, before you remove indexes, you need to figure out which query patterns use them. You could do this by removing them in production and observing where performance collapses. But that's unwise and will anger your users.
In my database, I have a set of tables with names such as table<N> where N is a non-negative integer. I need to find the table with the 'greatest' name in the lexicographical order. That is the name with the greatest N. SHOW TABLES lists all the tables, and I could not find a way to do it more efficiently than fetching the whole list in to the client and scanning it. Any suggestions?
Could you use the INFORMATION_SCHEMA.TABLES to achieve this?
Something like:
SELECT *
FROM INFORMATION_SCHEMA.TABLES
ORDER BY table_name DESC
LIMIT 1
You can, of course, add a WHERE clause to this too:
SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE table_name LIKE ('numberedTable%')
ORDER BY table_name DESC
LIMIT 1
if applicable.
I need to select all indexes from a given table.
But, it looks like MySQL creates multiple indexes with the same name with all the possible column combinations when there is more than one column in the index.
So SHOW INDEXES returns multiple times the same value.
Is there a way to do a select distinct to get the index names from a given table?
And if possible that is not MySQL specific.
Please try this query:
SELECT DISTINCT INDEX_NAME FROM information_schema.statistics
WHERE table_schema = 'your_schema'
AND table_name = 'your_table'
I have a database with about 200 tables, and need to do a query on all tables containing a certain column (creation_date), but not all tables have that column. SELECT * FROM * WHERE creation_date>=42 obviously doesn't work, but what would be the best way of doing it?
SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT, TABLE_NAME,
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'tbl_name'
[AND table_schema = 'db_name']
[AND column_name ='creation_date']
copied directly from MySQL - you need to loop thru these system tables and the list will contain those ...
then you can build your SQL statements and be sure that they work ...
You could build a dynamic SQL query from metadata. I would approach this like:
Get a list of tables
For each table,
See if the table has the column creation_date
If it does, add your query on this table to the dynamic query
Union the results together
You might also be able to create a view on the multiple tables. Then you can just query the view.
You can try to include the table you want to query like so:
SELECT * FROM table1 a, table2 b WHERE a.creation_date>=42
You cannot use a wildcard in the from. In the way as shown above you can specify on which tables the where clause must apply. You can than leave out the ones that don't have the column.
So in the example query, table1 (alias a) has the column, and table2 (alias b) does not.
Is there a MySQL command to count the number of rows in a table, and if so, what is it?
MySQL COUNT() function
SELECT COUNT(*) FROM table
A nice way to get AN ESTIMATE of the number of rows can be via meta-data information in the information_schema tables.
Note, this is just an ESTIMATE used for query optimizations.
There may be a way to get exact # of rows from the information_schema, but I am not 100% sure if this is possible.
SELECT table_schema, table_name, table_rows
FROM information_schema.tables
ORDER BY table_rows DESC
This gives you the number of rows from a specific table name that you set:
SELECT TABLE_ROWS from information_schema.TABLES where table_name = "your_specific_table_name_here";