What was your coolest SQL optimization, on a slow performing query?

后端 未结 15 2170
遥遥无期
遥遥无期 2021-02-10 06:45

Just speaking to a colleague of mine. He was walking with a hop in his step, on the way to the coffee machine.

I asked him \"what\'s with the \'swarmy\' walk?\", he said

相关标签:
15条回答
  • 2021-02-10 07:16

    I had a warm glow after being able to use a Cross Tab query to scrap oodles (technical term) of processing and lookups...

    Usually it's simple things like adding indexes or only getting the data you need, but when you find a problem that fits an answer you've seen before... good times!

    0 讨论(0)
  • 2021-02-10 07:16

    Changing order of conditions inside WHERE clause so it filters the most discriminating condition first (while at the same time indexes from non-discriminating columns like gender are removed).

    0 讨论(0)
  • 2021-02-10 07:20

    I had a query that was originally written for SQL Server 6.5, which did not support the SQL 92 join syntax, i.e.

    select foo.baz
    from foo
      left outer join bar
      on foo.a = bar.a
    

    was instead written as

    select foo.baz
    from foo, bar
    where foo.a *= bar.a
    

    The query had been around for a while, and the relevant data had accumulated to make the query run too slow, abut 90 seconds to complete. By the time this problem arose, we had upgraded to SQL Server 7.

    After mucking about with indexes and other Easter-egging, I changed the join syntax to be SQL 92 compliant. The query time dropped to 3 seconds.

    I don't think I'll ever have that feeling again. I was a f%$^ing hero.

    0 讨论(0)
  • 2021-02-10 07:21

    I answered this on a previous question ("Biggest performance improvement you’ve had with the smallest change?"), however, it's such a simple improvement, yet one that is and can be so often overlooked, that it bears repeating:

    Indexes!

    0 讨论(0)
  • 2021-02-10 07:23

    (Half way of topic)

    I rewrote a 3000 line stored procedure into LINQ2SQL/C#. The stored procedure juggled lots of data between a bunch of unindexed temp tables. The LINQ2SQL version read the data into a couple of Dictionaries and ILookups and then I joined the data manually with plain old C# code.

    The stored procedure took about 20 seconds and the LINQ2SQL/C# version took 0.2 seconds.

    0 讨论(0)
  • 2021-02-10 07:24

    Using SQL's BULKIMPORT to reduce several hours of inherited INSERT code to less than a minute.

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