Is there a better way instead of SELECT COUNT(*) statement to count the number of records in table?
Sometimes we have to count billions of records from temporary tables that are imported using bcp query.
Using count(*) or count(some_column) is the fastest way to check for a tables record count.
If you don't need to filter, the following query works well:
SELECT sum(rows) FROM SYS.PARTITIONS WHERE object_id=object_id('MY_TABLE') and index_id in(0,1)
That checkes the number of rows sql server is storing for that object. It can't return any data with the count, and there is no way to include a group by or where.
Related
Get other columns that correspond with MAX value of one column?
I know that when SELECT, sql usually gives all rows from the table(without WHERE condition).
but why this time sql gives data of only the first row for other columns(video_category, url, ...)?
does MAX() changes the behavior of SELECT?
if so, why the rest of columns are not derived from row of MAX(id)?
If you want all columns from the record having the max id, then you will have to use a subquery:
SELECT *
FROM yourTable
WHERE id = (SELECT MAX(id) FROM yourTable);
Your current output is only one record because when say take the max over the entire table, you are no longer speaking of individual records. I am guessing that you are using MySQL, in which case the values you see for the other columns were chosen by the database. But there is no guarantee about which record was chosen.
Some versions of SQL (e.g. MySQL, SQL Server), support LIMIT/TOP functionality which might simplify things. For example, on SQL Server we can just write:
SELECT TOP 1 *
FROM yourTable
ORDER BY id DESC;
I'm using Metabase for analytics. Sometimes it counts table rows.
I catch its query
SELECT count(*) AS "count" FROM "dbo"."TableName"
But the problem is that TableName isn't a table, it's a view.
This view is a union of many tables. That query runs very long time. Instead of SELECT COUNT I'd like to use
SELECT row_count FROM sys.dm_db_partition_stats
In that case, I get the result instantly.
Is there any possibility to replace metabase's query with my query?
Thanks
I have an application for query management. Previously I was using SQL Server database, and to get the number of affected rows by a query I used to do:
SELECT * FROM TABLE (or any other select query)
and then I do SELECT ##ROWCOUNT to get the number of rows affected by the last executed query.
I have read about SQL%ROWCOUNT, but I am not able to make it work in a SELECT statement
Is there any way to do this in a Oracle database?. Thank you!
Edited:
I have solved this by doing SELECT COUNT(*) FROM (QUERY) to get the number of rows affected by the query, I discarted this approach because it was not working in SQL Server if the query contains an ORDER BY clause.
I don't know of any exact equivalent in Oracle that you can use in pure SQL.
An alternative that may work for you, depending on your specific need, is to add a count(*) over () to your select statement to give you the total number of rows. It would at least save you from having to re-execute the query a 2nd time.
select t.*,
count(*) over () as num_rows
from table t
where ...
Or, if you can't change the original query, then you can wrap it like this:
select t.*,
count(*) over () as num_rows
from (query) t
I have a complex SQL query with multiple sub queries. The Query returns a very big data. The tables are dynamic and they get updated every day. Yesterday, the query didn't execute, because one of the subqueries returned multiple rows.
The subquery would be something like this.
Select Value1 from Table1 where Table1.ColumnName = 123456
Table1.ColumnName will be fetched dynamically, nothing will be hardcoded. Table1.ColumnName will be fetched from another subquery which runs perfectly.
My Question would be,
How to find which value in the particular subquery returned two rows.
How to find which value in the particular subquery returned two rows.
You need to check each sub-query whether it returns a single-row or multiple-rows for a value. You can use the COUNT function to verify -
select column_name, count(*) from table_name
group by column_name
having count(*) > 1
The above is the sub-query for which it checks the count of rows grouped by each value, if any value returns more than one row, that value is the culprit.
Once you get to know which sub-query and respective column is the culprit, you coulkd then use ROWNUM or ANALYTIC functions to limit the number of rows.
I need to get the total no of records in a table for pagination purpose..it has some around 1 million records...the count is calculated as part of page load..i can do count query but it takes little more time thus increasing the time to page load.So to avoid that i have used
select ROW_NUMS,OWNER from ALL_TABLES where table_name='table1'
But the problem is that this query for some tables gives result as zero and also sometimes the count is not correct(different from count query)
any idea how to update the all_tables data for a table and how all_table
If You want the exact number of records in a table, You need to select count(*). This result will be correct. NUM_ROWS has never been to provide the exact number of rows.
You can gather the table, but this will give you the estimate count.
exec dbms_stats.gather_table_stats('<OWNER>', '<TABLE_NAME>');
To get an accurate row count you can get the database to do the heavy lifting before it sends the data to the client, e.g.:
SELECT t.*
,COUNT(*) OVER () AS row_count
FROM my_table t;
You need to run ANALYZE statement to properly update that column