Is there any simple way to get count data of single table from different databases(db names) with same schema.
I tried like this..
SELECT COUNT(id) AS db1users,
(SELECT COUNT(id) FROM DB2.users) AS db2users,(
SELECT COUNT(id) FROM DB3.users) AS db3users,
(SELECT COUNT(id) FROM DB4.users) AS db4users,
..........
..........
..........
FROM DB1.users;
I got the exact result but query becoming very large. Is there any simple way to get this..
Please help
Another option, that avoids the need for dynamic sql (and is far less expensive, hence much more scalable), would be to use MySQL the INFORMATION_SCHEMA.TABLES table. It has a column named TABLE_ROWS, whose specification is as follows:
TABLE_ROWS
The number of rows. Some storage engines, such as MyISAM, store the exact count. For other storage engines, such as InnoDB, this value is an approximation, and may vary from the actual value by as much as 40% to 50%. In such cases, use SELECT COUNT(*) to obtain an accurate count.
If this matches your requirement, then you can use a simple query loke:
SELECT table_schema, table_rows FROM information_schema.tables WHERE table_name = 'users'
The best way to do this would be via a scripting language (e.g. Python, Ruby, PHP); you'd execute a database query to get all the database names from your database, then create a SQL statement with all your select count(id) from...; once you've built the SQL statement, you'd execute it.
You can also do this in dynamic SQL inside MySQL; dynamic SQL is hard to write and debug, so I'm not a huge fan....
Related
I want to find tablename and row count of all the tables in a database in mysql and pgsql by using query. Is there any query to find this?
The SQL-standard INFORMATION_SCHEMA provides information about the structure of your database - which tables it has, etc. It does not, however, contain row counts.
At least for PostgreSQL you have at least two options for getting row counts:
Use an external program or a PL/PgSQL function that generates dynamic SQL using EXECUTE to do a SELECT count(*) FROM tablename for each table found in the INFORMATION_SCHEMA (excluding system tables); or
Run ANALYZE then get the approximate rowcounts from the PostgreSQL statistics tables. This approach is a lot faster, but is only getting an approximate table rowcount based on statistical sampling and estimation.
This has been discussed in detail for PostgreSQL here.
The approach of querying INFORMATION_SCHEMA for a table list and then looping over the tables doing count should be portable across all databases. Other approaches will likely require various degrees of database-specific code.
For postgresql:
SELECT
nspname AS schema,relname table_name,reltuples::int rows_count
FROM pg_class C
LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
WHERE
nspname NOT IN ('pg_catalog', 'information_schema') AND
relkind='r' and reltuples>0
ORDER BY relname ;
Is it possible to retrieve the count of the number of columns a query returns? This can be easily done with a bound scripting language such as php, but I'm looking for db only solution.
Example:
CountCols(SELECT 'a','b','c')
=> 3
CountCols(SELECT * FROM information_schema.session_variables)
=> 2
Would this work for you?
select
count(*)
from
`information_schema`.`columns`
where
`table_schema` = 'my_table_schema' and `table_name` = 'my_table_name';
You only need to use table_schema if the table name exists in more than one database.
Based on your response comment, you are looking to count a dynamic number of columns. You may be able to do this with a temporary table, but you cannot access the data of a temporary table without possibly installing a patch.
Of note, there is a similar outstanding SO question asking how to select columns from a temporary table.
Well if you want to know the columns in a table just do:
DESCRIBE `table_name`
Otherwise there is no "real" way to get the number of columns in a select query since other than selecting * you select certain columns --> so you will know how many columns you are selecting.
You'll find your answer here most likely: http://dev.mysql.com/doc/refman/5.0/en/columns-table.html
Write a query off of that that takes a table name param and then query for columns of that table and sum that up.
I have a big table (approx. 150 M rows), and if I try to run a simple select count(*) on it, then mysql works for about an hour and then throws an error.
I'm assuming this is not due to a limitation of mysql, rather a problem on my side, but I have no idea where to start looking. any ideas?
the table is innodb
mysql 5.5 on linux
you can check it with table status like this
SHOW TABLE STATUS FROM db_name LIKE 'table_name';
you see the rows column....
You can use count(1) instead of count(*)
Try ::
Select count(1) from my_table
The easiest way to speed up queries like this is with a covering index. This will allow you to scan through the rows you want, but require fewer bytes of I/O per row (since you're only scanning a portion of each row's data, not the entire row). Furthermore, if your index is sorted in the same way that your query is, you can avoid the cost of a sort and can scan vastly fewer rows.
You can use Information Schema.
SELECT TABLE_ROWS FROM INFORMATION_SCHEMA.TABLES WHERE table_schema =
YOUR_DB_NAME AND table_name = YOUR_TABLE_NAME
I have a table using innodb. I know the table has roughly 89 million rows. Using
SELECT COUNT(*) FROM table;
takes about five minutes to run. I know that innodb is not optimized for unconditional COUNT(*) queries. How can I restructure the query to give me a count faster? Would just adding WHERE 1=1 work, or do I need to query a specific field?
I know I can get the approximate number of rows using SHOW TABLE STATUS, but I want to get the value in a PHP script, and it seems like there is a lot to dig through using that method.
If you are OK with the estimated number and just don't want to mess with running SHOW TABLE STATUS from PHP, you can use the information_schema DB:
SELECT TABLE_ROWS FROM information_schema.tables
WHERE TABLE_SCHEMA = 'my_db_name'
AND TABLE_NAME = 'my_table_name';
If you are ok with approximate number of records, you can use output of "explain".
Simplified verion of the code is
$result = mysql_query('explain SELECT count(*) from TABLE_NAME');
$row = mysql_fetch_assoc($result);
echo $row['rows'];
If the table is read frequently and updated infrequently, you may want to consider creating a statistics table that is updated via triggers when making changes to the table.
mysql_num_rows may be useful to you.
What is the MySQL command to retrieve the count of records in a table?
SELECT COUNT(*) FROM fooTable;
will count the number of rows in the table.
See the reference manual.
Because nobody mentioned it:
show table status;
lists all tables along with some additional information, including estimated rows for each table. This is what phpMyAdmin is using for its database page.
This information is available in MySQL 4, probably in MySQL 3.23 too - long time prior information schema database.
The number shown is estimated for InnoDB and TokuDB but it is absolutely correct for MyISAM and Aria (Maria) storage engines.
Per the documentation:
The number of rows. Some storage engines, such as MyISAM, store the
exact count. For other storage engines, such as InnoDB, this value is
an approximation, and may vary from the actual value by as much as 40%
to 50%. In such cases, use SELECT COUNT(*) to obtain an accurate
count.
This also is fastest way to see the row count on MySQL, because query like:
select count(*) from table;
Doing full table scan what could be very expensive operation that might take hours on large high load server. It also increase disk I/O.
The same operation might block the table for inserts and updates - this happen only on exotic storage engines.
InnoDB and TokuDB are OK with table lock, but need full table scan.
We have another way to find out the number of rows in a table without running a select query on that table.
Every MySQL instance has information_schema database. If you run the following query, it will give complete details about the table including the approximate number of rows in that table.
select * from information_schema.TABLES where table_name = 'table_name'\G
Simply:
SELECT COUNT(*) FROM `tablename`
select count(*) from YourTable
If you have several fields in your table and your table is huge, it's better DO NOT USE * because of it load all fields to memory and using the following will have better performance
SELECT COUNT(1) FROM fooTable;
Just do a
SELECT COUNT(*) FROM table;
You can specify conditions with a Where after that
SELECT COUNT(*) FROM table WHERE eye_color='brown';
As mentioned by Santosh, I think this query is suitably fast, while not querying all the table.
To return integer result of number of data records, for a specific tablename in a particular database:
select TABLE_ROWS from information_schema.TABLES where TABLE_SCHEMA = 'database'
AND table_name='tablename';
If you have a primary key or a unique key/index, the faster method possible (Tested with 4 millions row tables)
SHOW INDEXES FROM "database.tablename" WHERE Key_Name=\"PRIMARY\"
and then get cardinality field (it is close to instant)
Times where from 0.4s to 0.0001ms
$sql="SELECT count(*) as toplam FROM wp_postmeta WHERE meta_key='ICERIK' AND post_id=".$id;
$total = 0;
$sqls = mysql_query($sql,$conn);
if ( $sqls ) {
$total = mysql_result($sqls, 0);
};
echo "Total:".$total;`
You have to use count() returns the number of rows that matches a
specified criteria
select count(*) from table_name;
It can be convenient to select count with filter by indexed field. Try this
EXPLAIN SELECT * FROM table_name WHERE key < anything;