General rules for simplifying SQL statements

前端 未结 8 1542
心在旅途
心在旅途 2020-12-22 17:15

I\'m looking for some \"inference rules\" (similar to set operation rules or logic rules) which I can use to reduce a SQL query in complexity or size. Does there exist some

8条回答
  •  太阳男子
    2020-12-22 17:40

    • I suppose the obvious one is look for any Cursors that can be replaced with a SQL 'Set' based operation.
    • Next on my list, is look for any correlated sub-queries that can be re-written as a un-correlated query
    • In long stored procedures, break out separate SQL statements into their own stored procedures. That way they will get there own cached query plan.
    • Look for transactions that can have their scope shortened. I regularly find statements inside a transaction that can safely be outside.
    • Sub-selects can often be re-written as straight forward joins (modern optimisers are good at spotting simple ones)

    As @Quassnoi mentioned, the Optimiser often does a good job. One way to help it is to ensure indexes and statistics are up to date, and that suitable indexes exist for your query workload.

提交回复
热议问题