What are your most common sql optimizations?

后端 未结 17 1576
谎友^
谎友^ 2020-12-12 14:51

What are your most common SQL optimization that you used?

相关标签:
17条回答
  • 2020-12-12 15:15

    Couple of hints: Use

    delete from table where id>=1 and id<=3;
    

    instead of

    delete from table where id=1;
    delete from table where id=2;
    delete from table where id=3;
    

    Also use 'IN' instead of 'OR' syntax

    0 讨论(0)
  • 2020-12-12 15:16

    My favorite list of tips (explained in detail here) is as follows

    1. Try to restrict the queries result set by using the WHERE clause.
    2. Try to restrict the queries result set by returning only the particular columns from the table, not all the table's columns.
    3. Use views and stored procedures instead of heavy-duty queries.
    4. Whenever possible, try to avoid using SQL Server cursors.
    5. If you need to return the total table's row count, you can use an alternative way instead of the SELECT COUNT(*) statement.
    6. Try to use constraints instead of triggers, whenever possible.
    7. Use table variables instead of temporary tables.
    8. Try to avoid the HAVING clause, whenever possible.
    9. Whenever possible, try to avoid using the DISTINCT clause.
    10. Include SET NOCOUNT ON statement into your stored procedures to stop the message indicating the number of rows affected by a T-SQL statement.
    11. Use select statements with the TOP keyword or the SET ROWCOUNT statement if you need to return only the first n rows.
    12. Use the FAST number_rows table hint if you need to quickly return 'number_rows' rows.
    13. Try to use UNION ALL statement instead of UNION, whenever possible.
    14. Do not use optimizer hints in your queries.
    0 讨论(0)
  • 2020-12-12 15:17

    1) I've yet to find a situation where

    SELECT Field1, Field2, (SELECT Count(*) FROM tblLinked WHERE tblLinked.Field3 = tblSource.Field3) AS TheTotal
    FROM tblSource
    

    isn't improved by a LEFT JOIN to a derived table.

    SELECT Field1, Field2, IsNull(Linked.TheTotal,0) AS TheTotal
    FROM tblSource
    LEFT JOIN (SELECT Field3, Count(*) AS TheTotal
        FROM tblLinked
        GROUP BY Field3) AS Linked ON tblSource.Field3 = Linked.Field3
    

    2) Don't sort the results on the server unless the consuming app is unable to do this itself. This applies less often to web apps, but for desktop apps the client PC usually has plenty of power available and can happily do a sort.

    3) Use EXISTS instead of checking the Count of matching entries.

    4) Don't get obsessed with doing a query in just one SELECT clause. Judicious use of table variables (and sometimes temporary tables) can massively reduce the rows processed.

    0 讨论(0)
  • 2020-12-12 15:18

    Making sure tables are being joined in the correct order.

    0 讨论(0)
  • 2020-12-12 15:20

    I have read all answers and I didn't found LIMIT and OFFSET usage hints. It is very common usage in pagination with "prev" and "next" links. But rendering such a display can consume more resources than the entire rest of the site. When offsetting large number items, query can become very slow. So avoid these queries.

    • Do not count total items.
    • Show only "n" number first items (for example only top 100).

    Such methods uses Google, Twitter and other sites. In Google search there is no accurate number of results. There is only approximate number. Twitter doesn't allow user to view all past tweets. It shows only last n number (I can't remember how much).

    There is some link from MySQL performance blog.

    0 讨论(0)
提交回复
热议问题