Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Guys,

How to query and retrieve result faster? Before this, query quite acceptable to wait. Currently, it executing for 10 minutes and still querying. My query is to select count from 5 tables located at Azure SQL. I want to do data compare with Local Server SQL.
SQL
Dim CommandText As String = "WAITFOR DELAY '0:0:01';SELECT
(SELECT COUNT(*) FROM [dbo].[TableFirst] WHERE LineIDKey=" & idkey & ") as TableFirst,
(SELECT COUNT(*) FROM [dbo].[TableSecond] WHERE LineIDKey= " & idkey & ") As TableSecond,
(SELECT COUNT(*) FROM [dbo].[TableThird] WHERE LineIDKey=" & idkey & ") as TableThird,    
(SELECT COUNT(*) FROM [dbo].[TableForth] WHERE LineIDKey=" & idkey & ") as TableForth,
(SELECT COUNT(*) FROM [dbo].[TableFifth] WHERE LineIDKey=" & idkey & ") as TableFifth"

The LineIDKey is Number.

I have try this query which I found on internet.. Super-massive faster!
SQL
SELECT T.name AS TableName, 
       I.rows AS TotalCount 
FROM   sys.tables AS T 
       INNER JOIN sys.sysindexes AS I 
               ON T.object_id = I.id 
                  AND I.indid < 2
ORDER  BY T.name asc

But how to insert Condition Statement? This query show all tables from the database. Which suite what I want. By pending in:-
1. Insert condition of WHERE LineIDKey=" & idkey & "
2. Grab rows TableCount and TableName


#Note: All table in the database have LineIDKey column

What I have tried:

Dim CommandText As String = "WAITFOR DELAY '0:0:01';SELECT
(SELECT COUNT(*) FROM [dbo].[TableFirst] WHERE LineIDKey=" & idkey & ") as TableFirst,
(SELECT COUNT(*) FROM [dbo].[TableSecond] WHERE LineIDKey= " & idkey & ") As TableSecond,
(SELECT COUNT(*) FROM [dbo].[TableThird] WHERE LineIDKey=" & idkey & ") as TableThird,
(SELECT COUNT(*) FROM [dbo].[TableForth] WHERE LineIDKey=" & idkey & ") as TableForth,
(SELECT COUNT(*) FROM [dbo].[TableFifth] WHERE LineIDKey=" & idkey & ") as TableFifth"



SELECT T.name AS TableName,
I.rows AS TotalCount
FROM sys.tables AS T
INNER JOIN sys.sysindexes AS I
ON T.object_id = I.id
AND I.indid < 2
ORDER BY T.name asc
Posted
Updated 28-Aug-16 22:42pm
Comments
Mehdi Gholam 29-Aug-16 1:09am    
Try adding an index on LineIDKey
Luiey Ichigo 29-Aug-16 2:03am    
index? how to add it to current query sir?
Mehdi Gholam 29-Aug-16 2:18am    
Start here : https://azure.microsoft.com/en-us/documentation/articles/sql-database-advisor/

 
Share this answer
 
You probably need to review your database design by adding an index on field LineIDKey, it will greatly improve the performance of the query.
SQL CREATE INDEX Statement[^]

The way you pass value of idkey in the query is bad because it open the door to code injection, use parameter instead
SQL Injection[^]

This site contain a good tuto for SQL
SQL Tutorial[^]
 
Share this answer
 
Comments
Luiey Ichigo 29-Aug-16 6:00am    
I guess something bad because I use SSMS and Export Data to Azure SQL but the copied data and table creation from LocalSQL does not passing the primary and foreign key..is it legit as what I'm facing now?
Patrice T 29-Aug-16 6:06am    
I know SQL enough for my answer, but not for this.
Use Improve question to update your question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900