Programmer
Programmer

Reputation: 6753

SQL query to search faster or using hash table

If I am looking for a record in the database, is writing a sql query to search the database directly faster OR is reading the entire data from the database into a hashtable and then searching in O(1) time faster? This question is for experienced programmers who have faced such issues in the past.

Upvotes: 0

Views: 4991

Answers (5)

Farhan surahio
Farhan surahio

Reputation: 1

Despite, DBMS is being considered well convenient environment as compare to hash table. if you are trying to get results with few thousands of records then you do not have need to create index. it depends on need. Thus, it is much easy to get answers from remote machine with three tier applications. it takes care about row count, IO Speed etc.

Upvotes: 0

Farhan surahio
Farhan surahio

Reputation: 1

Sql Server Database is more faster and better than Hash-table. one important reason behind. Hash table reads the data once from secondary storage and then loaded into memory. now, it is easy to identify that what will happen? By Storing data in a huge manner, system will be slow. it will difficult to manipulate and retrieve the records.....

Upvotes: 0

Majid
Majid

Reputation: 151

Making direct sql query to database would obviously be much faster, than first reading all the records into a hash table and searching from it. This will not only save your time in loading all the records firstly into a hash table and then searching through them. 2ndly it will also save lots of memory, that your hash tables will consume.

I have experienced this kind of situations. Hope this helps you!

Upvotes: 1

Utku Zihnioglu
Utku Zihnioglu

Reputation: 4873

If the SQL table is not indexed you'd have to benchmark to find your answer. Since there are lots of factors such as the row count, IO speed, network speed (if database is on a remove machine), it is hard to just give an answer to the question

On the other hand, indexing the table is a better choice. Just, leave the DBMS's job to DBMS.

Upvotes: -1

user330315
user330315

Reputation:

If you know the primary key of the row or the column you are searching on is indexed, then doing the retrieval" using SQL will be much faster. Especially if your table does not fit into memory.

Upvotes: 2

Related Questions