Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

SQL-query slow in SQL Server CE but fast in SQL Server

I'm working on a product where I need to support both SQL Server and SQL Server CE (4.0).

I have two tables where one is an entity and the other is relation-table, something like this:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First of all, thanks a lot to @ErikEJ who was kind enough to help me out privately. Such a nice and helping person!

ErikEJ pointed out one thing that I did not know that SQLCE only uses one index per table and question which might be good to know for the future.

After fiddling around and reading up on this I came up with the idea to try a "CROSS APPLY"-query.

Something like this:

SELECT [id]
      ,[name]
      ,ot.total as totalOrders
  FROM [customer] as c
  CROSS APPLY (SELECT Count(o.id) as total FROM orders AS o WHERE o.customerId = c.id) as ot
ORDER BY id
OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY

This query executes in about 1ms on SQL Server and somewhere around 300ms on SQLCE. In terms of performance, in this case, it's still around 225% faster to fetch the first 50 rows and based on them fetch the COUNT() with a group query. But if one needs to sort on the count (which id need) the CROSS APPLY-query seems to be the best option.

Thanks to everyone who took the time to help me!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...