Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a database column that has numbers need to be indexed?

Suppose Bob has earned 107 points, Mary 105 points and John also 105 points. These numbers could change and are not unique. Now I want to query and check the order, who is on top and who is on the bottom. Does that column that holds points need to be indexed ?

like image 781
user1861388 Avatar asked Oct 29 '25 09:10

user1861388


2 Answers

It depends on the database system used and the size (number of rows) of the table.

If the table is small enough and you're using MSSQL for example, a table scan will be used (the entire table will be read into memory) and an index will be relatively useless.

Generally speaking, yes - the field should be indexed if that is the column you'll be using to select or sort data.

like image 194
Dan Esparza Avatar answered Oct 31 '25 00:10

Dan Esparza


As a very general rule, I consider indexing columns that appear in the WHERE or ORDER BY clauses of commonly executed queries.

like image 44
JSR Avatar answered Oct 31 '25 01:10

JSR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!