row num gives zero records - sql

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

Related

how to get all row count of a table from rowcount after having top row filter

I have a huge table in my database and a stored procedure accessing it, which needs pagination.
To achieve this I want total records of the table, and for that, I am facing performance issue because for doing that I need to run this query twice:
First time to get count for all records
Secondly when I need to select records in that page range
Is there any way I can avoid the first query for getting the total count instead of I can use row count or something else?
One way to do it would be something like this:
SELECT
(your list of columns),
COUNT(*) OVER ()
FROM
dbo.YourTable
ORDER BY
(whatever column you want to order by)
OFFSET x ROWS FETCH NEXT y ROWS ONLY;
With the OFFSET / FETCH, you retrieve only a page of data - and the COUNT(*) OVER() will give you the total count of the rows in the table - all in a single query

using SQL COUNT function or executing search query directly which is more efficient

Let's say i have a very big database , if i execute a search query directly then count the returned rows would it be more faster ? Or using COUNT(searchquery) then start executing query like ->
SELECT *
FROM TABLE
WHERE bla='blabla'
OFFSET 0 FETCH NEXT 20 ROWS ONLY
I searched for it but i couldn't find any solution.
Do the count in the database! It will be much faster.
First, a count(*) only returns one row and one value. That is much, much less data -- and much faster -- than returning all the rows.
Second, a count(*) does not reference any columns in the select, so the query can be better optimized. It might be possible to get the count without ever looking at the data pages.
It looks like you are doing paging. You need the total count to do display the total count and calculate the total number of pages to the user, yes?
Than Gordon's answer is the one to use.

SQL Query Execution time , SQL Server, Nested Query

I have a query as following:
SELECT Brand,Id,Model FROM product
Which takes time in order of seconds as Product table has more than 1 million records.
But the query executes within no time. (less than even one second))
select count(*) as numberOfRows from (SELECT Brand,Id,Model FROM product) result
Why is that?
When you execute a Query, the Time taken will vary depending on the Number of Columns and Rows and their datatypes.
In a table where you have 10 Columns, The Performance will be different if you select all columns (*) for all records and Just 1 or Two Columns for All records.
Because The Amount of data loaded is less in the Second case, it will execute faster.
Just like that, When you say Count(*) the result is Just a single Cell whereas in your first Select, you are selecting Millions of rows for those 3 columns, So the amount of Data is high.
That's why you are getting the Count(*) result faster. You don't need to give * inside the count, Instead Just use Count(1) for even more better performance.

I am using select top 5 from(select * from tbl2) tbl, will it get all records from tbl2 or will it get only specific records i.e 5

I am using select top 5 from(select * from tbl2) tbl, will it get all records from tbl2 and then it will it get only specific records i.e 5? or do it only get 5 records from internal memory. suppose we have 1000 records in tbl2.
For future reference, SQL actually has a huge amount of amazing documentation/resources online. For instance: This google search.
As to your question, it will pull the top five results matching your criteria, so it depends. It'll go through, and find the first five results matching your criteria. If those are the last results it goes through, it'll still have to do the comparisons and filtering on all rows, the only difference will be that it will have to send less rows to your computer.
For example, let's say we have a table my_table with two columns: Customer_ID (which is unique) and First_Purchase_Date (which is a date value between 2015-01-01 and 2017-07-26). If we simply do SELECT TOP 5 * FROM my_table then it will go through and pull the first five rows it finds, without looking at the rest of the rows. On the other hand, if we do SELECT TOP 5 * FROM my_table WHERE First_Purchase_Date = '2017-05-17' then it will have to go through all the rows until it can find five rows with a First_Purchase_Date of 2017-05-17. If First_Purchase_Date is indexed, this should not be very expensive, as it'll more or less know where to look. If it's not, then it depends on your how SQL has decided to structure your table, and if it has created any useful statistics. Worst case, it could not have any statistics and the desired rows could be the last five in the database, in which case it will have to complete the comparison on all the rows in the database.
By the way, this is a somewhat poor idea, as the columns returned will not necessarily stay consistent over time. It may be a good idea to throw in an ORDER BY clause, to ensure you get the same records every time.
The SELECT TOP clause is used to specify the number of records to return.
It limits the rows returned in a query result set to a specified number of rows or percentage of rows. When TOP is used in conjunction with the ORDER BY clause, the result set is limited to the first N number of ordered rows; otherwise, it returns the first N number of rows in an undefined order.
SELECT TOP number|percent column_name(s)
FROM table_name
WHERE condition;

Get total number of rows while using limit clause

I am querying my table to achieve pagination but I do not know the total number of rows in the table.
select name from table where id = 1 limit 0, 10
Is there a way to find out the total number of rows that would have returned if I had not used limit clause without querying for total count.
SQLite computes results on the fly when they are actually needed.
The only way to get the total count is to run the actual query (or better, SELECT COUNT(*)) without the LIMIT.
Depends on which back end technology you are using. In PHP, mysql_num_rows() returns number of rows without actually fetching the data.