What are your most common sql optimizations?

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

What are your most common SQL optimization that you used?

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

    The two most important things in my experience are fewer joins and less queries. Other than those there's lots of DB specific stuff, COUNT(*) is relatively slow on PgSQL, subselects are dog slow on MySQL, etc.

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

    The best optimisation I've ever had using SQL was to really understand what was needed to be done the data and REMOVE ton's of SQL from the query.

    The fastest query is the query that does not have to be run.

    REALLY THINK about what your doing to the data. Are you working row-by-row? (then use set based code).

    • Do you really need to join to all those tables?

    • Can two small (simple) queries do the job better and quicker than a single large query?

    • If you combine these two queries into a single query can it run faster?

    Finally, PROFILE your queries (EXPLAIN PLAN or SQL PROFILER) and look at the "IO gets". Generally you want to reduce the number of GET's to a ratio something like 10 gets per output row.

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

    The biggest optimizations i used recently where quite simple.

    Keep as much of the business logic as close as possible to the sql server. Aka keep you business code on the same machine as the sql server. Let your business logic return as little as possible code back to the final client.

    Keep your SQL query's as 'short as possible' like Frost said, use single update statements over multiple statements.

    Only use transactions when you need them

    Create temp tables for partial joins to speed up the joins (dont forget to index them)

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

    Avoid the use of onboard functions like convertdate, stringreplace and such in your Views. If you can't make sure that the data is in a valid format use stored procedures which run reguallary to 'clean up' the data in your relevant tables.

    this is nasty, but it saves the views time i.e. keeps the user happy... ^^

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

    Index foreign keys!

    Maybe this isn't a sql query syntax optimisation, but more a storage optimisation. But i see it re-occur all the time & its a pet peeve.

    0 讨论(0)
  • 2020-12-12 15:14
    1. indexes its the most common optimization
    2. De normalizing the tables.
    3. Removing constraints (only if you know what you are doing)
    0 讨论(0)
提交回复
热议问题