1

I have a database table tblDetails with following fields:

itemID(int)(primary), itemCode(varchar), itemName(varchar),itemDescription(varchar)

Now this table has more than 50,000 rows and will keep increasing. When the user enters a itemCode, the query should go through the entire table to check if the itemCode entered by the user is valid or not. So my concern is the time consumed in searching the database as the number of rows increases.

Is there a better way to search a database? Is there a better database design? How much time(approx.) will it take to query 50 thousand rows?

Please suggest.

4

3 回答 3

1

Create an index on itemCode, if itemCode is unique for your table, then make it a primary key, it will get a clustered index on it and will be much faster to access

于 2012-04-20T06:02:44.327 回答
1

if you set an index on itemCode, a search on that column will no longer be linear.

whatever database you're using should take the approach of a balanced tree for a search on that indexed column.

于 2012-04-20T06:05:16.420 回答
1

Others have already explained that you should put an index on itemCode, let me answer how much time it will take to search: the B-tree index on 50000 values will probably be about 3 levels deep, so it will take 3 disk reads to bring the relevant nodes in memory. Even a cheapo mechanical drive will be able to do about 100 reads per second, so your search will take about 1/30th of the second.

That's the worst case scenario, though. Once relevant pages are cached, you are likely to be able to search in 0 disk reads, which is essentially instantaneous.

BTW, 50000 is really small in the context of databases. Proper indexing will enable you to do really fast searching on orders of magnitude larger amount. B-tree on 5000000 values might be 4 levels or so deep, on 500000000 values 5 levels deep etc... (just example numbers, YMMV). This is a logarithmic dependency, meaning your search slows down much slower than the number of elements raises.

For more on the topic, I warmly recommend reading about Anatomy of an SQL Index.

于 2012-04-20T14:16:20.377 回答