SQL takes more time to fetch records - sql

I am using sql server 2012. I have one table, contains 1.7 million records. I am just selecting all record using (select * from table_name). But it takes 1 Hour 2 minutes to fetch.
What should i do to fetch records quickly?

All you can do is limit your Result by Using
Top(100) Result or Where Clause Your Desired Data instead of
SELECT * FROM table
This will help you get only concerning and limited data in a less amount of time.
Another thing you can do is to Get Concerning columns only which gives you desired results.
This will significantly enhance the fetching time.

Related

How to Optimize SQL query with DISTINCT and fetch first?

I am trying to fetch first 5 distinct rows using the query SELECT DISTINCT col_A from table_name fetch first 5 rows only;
But the table has millions of rows so using distinct is expensive as it scans the whole table for only 5 rows taking a lot of time, around 200 seconds for me.
Is there a workaround or subquery for this?
The problem here is you don't know if a row is unique unless you check all the other rows. I believe your only solution would be to index the rows such that non-distinct rows are indexed together. That might buy you some efficiency when searching, however it will cost you when inserting data.

Combine two SQL queries into one query for the same table

I have a table called COMPUTED_DATA. It contains 3 columns: data, cluster, and last_execution.
There is job which runs every 2 weeks which inserts multiple data for a cluster and its last_execution time.
My requirement is to get the data for a cluster for its most recent last_execution time. Currently I have written query like this in my code
last_execution = SELECT distinct(last_execution) FROM COMPUTED_DATA
WHERE cluster=1204 AND ORDER BY last_execution DESC limit 1
The above query gets me the most recent last_execution
data = SELECT data FROM COMPUTED_DATA WHERE cluster=1204 AND
last_execution={last_execution}
This query uses that last_execution to get the data.
My question is can this be combined into just 1 query. I am running this in my spark cluster so each SQL query is very time expensive.
Hence I want to combine this into one query. Please help.
EDIT: the second query where I am getting data from returns multiple rows. Hence I cannot use limit on the second query because that count is unknown.
Yes it can
SELECT
data
FROM
COMPUTED_DATA
WHERE
cluster = 1204 and last_execution=(SELECT distinct(last_execution) FROM COMPUTED_DATA
WHERE cluster=1204 AND ORDER BY last_execution DESC limit 1)
This isn't the most beautiful way to write this, but you get idea how to use subquery in where clause.

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.

Index Created but doesn't speed-up on retrieval process

I have created table as bellow
create table T1(num varchar2(20))
then I inserted 3 lac numbers in above table so now it looks like below
num
1
2
3
.
.
300000
Now if I do
select * from T1
then it takes 1min 15sec to completely fetch the records and as I created index on column num and if I use below query then it should be faster to fetch 3 lac records but it takes also 1min15sec for fetch the records
select * from T1 where num between '1' and '300000'
So how the index has improved my retrieval process?
The index does not improve the retrieval process when you are trying to fetch all rows.
The index makes it possible to find a subset of rows much more quickly.
An index can help if you want to retrieve a few rows from a large table. But since you retrieve all rows and since your index contains all the columns of your table, it won't speed up the query.
Furthermore, you don't tell us what tool you use to retrieve the data. I guess you use SQL Developer or Toad. So what you measure is the time it takes SQL Developer or Toad to store 300,000 rows in memory in such a way that they can be easily displayed on screen in a scrollable table. You aren't really measuring how long it takes to retrieve them.
To get a test of the effects of having an index in place you might want to try a query such as
SELECT *
FROM T1
WHERE NUM IN ('288888', '188888', '88888')
both with with the index in place, and again after removing the index. You should also collect statistics on the table prior to running the query with the index in place or you may still get a query which performs a full table scan. Share and enjoy.