Here are the 10 most effective ways to optimize your SQL queries. Indexing: Ensure proper indexing for quick access to the database. Select query: Specify the columns in SELECT query instead of SELECT* to avoid extra fetching load on the database. Running queries: Loops in query structure slows the sequence.
SQL is the most commonly used database language, and so it can be used for almost any company that needs to store relational data. Queries within SQL are used to retrieve data from the database, but the queries vary in efficiency.
Reducing the amount of data that is returned, by only returning the fields required and only returning the rows required. This is the most common, as you do it for every query that returns data.
Adding indexes. This is not done as frequently, as some tables doesn't need any other index than the one created for the primary key.
My favorite list of tips (explained in detail here) is as follows
By far and above: Making covering indexes
A covering index includes all the columns that the query will need, thereby avoiding the need to do lookups on the results of an index seek. This will then avoid the system feeling like a scan could be quicker (which is remarkably quick considering the cost of lookups).
But also worth mentioning:
Having an index that will allow a merge join. A MERGE join is able to occur when joining two tables that are ordered by the join conditions. But of course, in saying 'table', we really mean 'index', right...
Also - removing scalar functions and using table-valued functions instead... as scalar functions are not able to be simplified out.
Also - putting a unique index on a column that you know to be unique, allowing the query optimizer to use this knowledge to make better optimization choices. Also applies to NOT NULL constraints.
Also - using Binary collation when comparing strings that are in a known case, so that the system doesn't have to consider the different case options.
Of course I could go on all day...
Rob
Caching db output. Avoiding pressuring the database at all seems to be a prudent optimization.
+1 memcached.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With