Do Not Think That One Second is Fast for Query Execution
Databases are incredibly fast and simple queries like the one above should execute in virtually no time, even on mediocre servers.
Join the DZone community and get the full member experience.Join For Free
i keep encountering situations where rdbms users think that one second for query execution is anything near fast. most recently, in this stack overflow question:
the poster’s original question was why a similar query executes in one second when executed in sql server management studio whereas the (seemingly) same query executes in 60 seconds when executed from hibernate. the query looks similar to this:
select student_id from student_table where roll_no in ('a101','a102','a103',.....'a250');
there might be many reasons for this difference. most likely, there’s also a hibernate n+1 issue hidden somewhere. but the most striking message here is:
please do not believe that 1 second is fast.
databases are incredibly fast and simple queries like the one above should execute in virtually no time, even on mediocre servers. even on your laptop! markus winand has made it a point to tell you that in 80% of all performance issues, all you have to do is to add that missing index . and that’s the case here as well!
the poster’s original table contains only two indexes:
create table student_table ( student_id bigint not null identity , class_id bigint not null , student_first_name varchar(100) not null , student_last_name varchar(100) , roll_no varchar(100) not null , primary key (student_id) , constraint uk_studentunique_1 unique (class_id, roll_no) );
there is an index to implement the
, and there’s another index for the
constraint, but both indexes aren’t really very useful, as the query predicate filters on
, which is only the second column of the
constraint. when executing the above query on roughly 8 million rows, i’m getting an index scan on the
index and the query runs in three seconds:
this “index scan” operation is not good at all. i’m actually running through all of the index to find all the applicable
values in each index leaf node. this is explained well in
use the index luke’s page about concatenated indexes
but the good news is, sql server management studio gives you immediate tuning advice. just right click on the execution plan and choose “missing index details…” to get the following advice:
/* missing index details from sqlquery1.sql - lukas-envy\sqlexpress.test the query processor estimates that implementing the following index could improve the query cost by 87.5035%. */ /* use [test] go create nonclustered index [<name of missing index>] on [dbo].[student_table] ([roll_no]) include ([student_id]) go */
this doesn’t necessarily mean that the above index is the optimal choice for all your queries, but the fact that you’re querying using a predicate on
should be a strong-enough indicator that you should have an index on at least this
column. the simplest possible index here is simply:
create index i_student_roll_no on student_table (roll_no);
with that index in place, we’ll now get an “index seek” operation, which runs instantly:
in this particular case, a “covering index” as suggested by vlad mihalcea in his answer might be appropriate. for instance:
create index i_student_roll_no on student_table (roll_no, student_id);
the advantage of a covering index is that all the information needed for the query execution is already contained in the index . this is true in this particular case, but it can also be dangerous as:
- the covering index needs more space, and if the table is already large, space might become an issue
- the covering index only adds value as long as the query doesn’t also use additional columns (e.g. for projections, calculations, additional filtering, sorting, etc.). this is probably not the case in this simple example, which might change quickly in the near future
thus, a covering index shouldn’t be your default choice in such cases. better be conservative and add only those columns in the index, that add immediate value for filtering.
i’d like to repeat this time and again:
do not think that one second is fast
do not think that anything beyond 2-3ms is fast!
unless you’re doing heavy reporting or batch processing, where processing time obviously might take a bit longer, simple queries like these should never be anything slower than instant . and most of the time, you can achieve this speed by adding an index.
on a side-note
the poster of the aforementioned question obviously has other issues as well. none of the above explain the execution speed difference between hibernate and “plain sql”. but again, the message here is that if your “plain sql” already takes more than one second, you have a very low-hanging fruit to fix.
Published at DZone with permission of Lukas Eder. See the original article here.
Opinions expressed by DZone contributors are their own.