Remote Query fired from ServerA to ServerB. ServerB holds the datbase and Table. Table Name: BigDummy
NO of records 1.4 Million Approx
Three scenairos all of them using "Begin distributed tran". There is absolutely no issue when a simple "Begin tran" is being used.
Case 1.
Plain 4 part T-SQL Query.
Actual query issued:
SELECT TOP (1)*FROM DBA.TestDB.dbo.Bigdummy
ORDER
BYObject_iddesc
Profiler statistics for the above query:
Reads: 24245
Duration: 5181 Milliseconds
Query shown in profiler is NOTSAME as fired from mgt studio.
declare @p1int
set @p1=6
exec
sp_prepexec@p1 output,NULL,N'SELECT "Tbl1002"."name" "Col1005","Tbl1002"."object_id""Col1006","Tbl1002"."principal_id" "Col1007","Tbl1002"."schema_id" "Col1008","Tbl1002"."parent_object_id" "Col1009","Tbl1002"."type" "Col1010","Tbl1002"."type_desc""Col1011","Tbl1002"."create_date" "Col1012","Tbl1002"."modify_date" "Col1013","Tbl1002"."is_ms_shipped" "Col1014","Tbl1002"."is_published""Col1015","Tbl1002"."is_schema_published" "Col1003" FROM "TestDB"."dbo"."Bigdummy" "Tbl1002" ORDER BY "Col1006" DESC'
select @p1
Case 2:
SELECT*FROM
OPENQUERY(DBA,'SELECT TOP (1) * FROM TestDB.dbo.Bigdummy
ORDER BY Object_id desc')
Profiler statistics for the above query:
Reads: 24245
Duration: 192 Milliseconds
Query shown in profiler is SAME as fired from mgt studio.
SELECT TOP (1)*FROM TestDB.dbo.Bigdummy
ORDERBYObject_id desc
Case 3.
Exact same original query is wrapped in a proc
EXEC DBA.TestDB.dbo.readdummy
Profiler statistics for the above query:
Reads: 24245
Duration: 225 Milliseconds
Query shown in profiler isSAME as fired from mgt studio.
exec"TestDB"."dbo"."readdummy"
Profiler Screenshots for All three Cases:
Question: I am sure the first query is taking longer because it is not executed on the ServerB in extactly the way it was issued.
The Query GETS CHANGED on the remote server for case 1.
When I check the Sp:StmtCompleted, it shows the following query actually executed:
SELECT"Tbl1002"."name" "Col1005","Tbl1002"."object_id""Col1006"
,"Tbl1002"."principal_id" "Col1007","Tbl1002"."schema_id""Col1008"
,"Tbl1002"."parent_object_id" "Col1009","Tbl1002"."type""Col1010"
,"Tbl1002"."type_desc" "Col1011","Tbl1002"."create_date""Col1012"
,"Tbl1002"."modify_date" "Col1013","Tbl1002"."is_ms_shipped""Col1014"
,"Tbl1002"."is_published" "Col1015","Tbl1002"."is_schema_published"
"Col1003"FROM"TestDB"."dbo"."Bigdummy" "Tbl1002"ORDERBY"Col1006"
DESC
If I run the above query manually on serverB, it will take the same amount of time to complete.
My query is that why is the TOP (1) clause getting removed here. When I am running the remote query Like below on the ServerA:
BEGINDISTRIBUTEDTRAN A
SELECT TOP (1)*FROM DBA.TestDB.dbo.Bigdummy
ORDER
BYObject_iddesc
It is executing as below on serverB:
SELECT"Tbl1002"."name" "Col1005","Tbl1002"."object_id""Col1006"
,"Tbl1002"."principal_id" "Col1007","Tbl1002"."schema_id""Col1008"
,"Tbl1002"."parent_object_id" "Col1009","Tbl1002"."type""Col1010"
,"Tbl1002"."type_desc" "Col1011","Tbl1002"."create_date""Col1012"
,"Tbl1002"."modify_date" "Col1013","Tbl1002"."is_ms_shipped""Col1014"
,"Tbl1002"."is_published" "Col1015","Tbl1002"."is_schema_published"
"Col1003" FROM"TestDB"."dbo"."Bigdummy" "Tbl1002"ORDERBY"Col1006"
DESC