Difference in order of execution and design in SQL Server.

In summary: Is there a good reason why this is the case?Yes the sql engine constructs an execution tree and decides how to best execute the query.Some systems have a describe or explain command that will show how the query will be executed and the actual tables it will use.It can be quite surprising too. As an example you might have built an index table to speed your query only to discover that it’s not being used. This may mean a different index table is needed.One caveat is it can be a negative to create too many index tables especially in a high performance application where data is added and deleted constantly. Each time you add or drop data the associated index tables needed to be added to or deleted
  • #1
WWGD
Science Advisor
Gold Member
7,007
10,464
Hi, given Sql server executes queries order from, where, group by, having, select, order by. Why don't we write queries in same order? Would that improve speed of execution ?
 
Technology news on Phys.org
  • #2
No the sql engine constructs an execution tree and decides how to best execute the query.

Some systems have a describe or explain command that will show how the query will be executed and the actual tables it will use.

It can be quite surprising too. As an example you might have built an index table to speed your query only to discover that it’s not being used. This may mean a different index table is needed.

One caveat is it can be a negative to create too many index tables especially in a high performance application where data is added and deleted constantly. Each time you add or drop data the associated index tables needed to be added to or deleted from.
 
  • Like
Likes WWGD
  • #3
jedishrfu said:
No the sql engine constructs an execution tree and decides how to best execute the query.

Some systems have a describe or explain command that will show how the query will be executed and the actual tables it will use.

It can be quite surprising too. As an example you might have built an index table to speed your query only to discover that it’s not being used. This may mean a different index table is needed.

One caveat is it can be a negative to create too many index tables especially in a high performance application where data is added and deleted constantly. Each time you add or drop data the associated index tables needed to be added to or deleted from.
I guess that is why you do not use many indexes in OLTP setup? I mean, since they do mostly inserts, updates and deletes.
 
  • #4
jedishrfu said:
No the sql engine constructs an execution tree and decides how to best execute the query.

Some systems have a describe or explain command that will show how the query will be executed and the actual tables it will use.

It can be quite surprising too. As an example you might have built an index table to speed your query only to discover that it’s not being used. This may mean a different index table is needed.

One caveat is it can be a negative to create too many index tables especially in a high performance application where data is added and deleted constantly. Each time you add or drop data the associated index tables needed to be added to or deleted from.
It seems from doing informal research that SQL Server will not execute queries that are not done in the order : SELECT FROM WHERE GROUP BY HAVING ORDER BY. I get error messages every time I try any query that violates this order sequence.
 

1. What is the difference between order of execution and design in SQL Server?

The order of execution refers to the sequence in which SQL statements are processed by the database engine. This is determined by the logical processing order, which can be affected by factors such as the use of subqueries or the presence of nested views. On the other hand, design in SQL Server refers to the structure and organization of the database, including tables, columns, indexes, and relationships.

2. How does the order of execution affect the design of a database in SQL Server?

The order of execution can impact the performance and efficiency of a database design. For example, if a query is frequently executed and involves multiple subqueries, it may be beneficial to redesign the database by creating views or indexes to improve the logical processing order and reduce execution time.

3. Can the order of execution be changed in SQL Server?

The logical processing order in SQL Server is determined by the database engine and cannot be directly changed. However, database designers and developers can optimize their database design to influence the order of execution and improve performance.

4. How does the order of execution affect the result of a query in SQL Server?

The order of execution can impact the result of a query in SQL Server, particularly when the query involves complex logic or joins. If the logical processing order is not optimized, the result may be incorrect or take longer to retrieve.

5. Can the design of a database affect the order of execution in SQL Server?

Yes, the design of a database in SQL Server can impact the order of execution. For example, if a table is heavily indexed, it may improve the logical processing order and result in faster execution of queries. Similarly, a poorly designed database with redundant or unnecessary tables can lead to a less efficient order of execution.

Similar threads

  • Programming and Computer Science
2
Replies
51
Views
4K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
18
Views
3K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
7
Views
429
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
7
Views
471
  • STEM Academic Advising
Replies
1
Views
681
  • Programming and Computer Science
Replies
28
Views
719
Back
Top