Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the algorithm for query search in the database?

Good day everyone, I'm currently doing research on search algorithm optimization.

As of now, I'm researching on the Database.

In a database w/ SQL Support.

I can write the query for a specific table.

  1. Select Number from Table1 where Name = "Test";
  2. Select * from Table1 where Name = "Test";

1 searches the number from Table1 from where the Name is Test and 2 searches all the column for name Test.

I understand the concept of the function however what I'm interested in learning what is the approach of the search?

Is it just plain linear search where from the first index until the nth index it will grab so long as the condition is true thus having O(n) speed or does it have a unique algorithm that speeds its process?

like image 875
Treize Avatar asked Nov 13 '12 14:11

Treize


People also ask

Which algorithm is used for database searches?

Linear search algorithms check every record for the one associated with a target key in a linear fashion. Binary, or half-interval, searches repeatedly target the center of the search structure and divide the search space in half.

What searching algorithm does SQL use?

SQL Server's algorithm, which searches the index tree to find the exact row in the leaf page, uses the extra complexity of the index to great advantage, making the index faster than either the O(N/2) table scan or an O(Log2N) binary search.

What is the search algorithm?

A search algorithm is the step-by-step procedure used to locate specific data among a collection of data. It is considered a fundamental procedure in computing. In computer science, when searching for data, the difference between a fast application and a slower one often lies in the use of the proper search algorithm.

Is a SQL query an algorithm?

s sql is an algorithimic language but there is a lots of queries and as a alg there is a such type of procedure in writing the queries and languages and certain constraints can be applied for that.


1 Answers

Well, it depends on how the data is stored and what are you trying to do.

  • As already indicated, a common structure for maintaining entries is a B+ tree. The tree is well optimized for disk since the actual data is stored only in leaves - and the keys are stored in the internal nodes. It usually allows a very small number of disk accesses since the top k levels of the tree can be stored in RAM, and only the few bottom levels will be stored on disk and require a disk read for each.
  • Other alternative is a hash table. You maintain in memory (RAM) an array of "pointers" - these pointers indicate a disk address, which contains a bucket that includes all entries with the corresponding hash value. Using this method, you only need O(1) disk accesses (which is usually the bottleneck when dealing with data bases), so it should be relatively fast.
    However, a hash table does not allow efficient range queries (which can be efficiently done in a B+ tree).

The disadvantage of all of the above is that it requires a single key - i.e. if the hash table or B+ tree is built according to the field "id" of the relation, and then you search according to "key" - it becomes useless.
If you want to guarantee fast search for all fields of the relation - you are going to need several structures, each according to a different key - which is not very memory efficient.

Now, there are many optimizations to be considered according to the specific usage. If for example, number of searches is expected to be very small (say smaller loglogN of total ops), maintaining a B+ tree is overall less efficient then just storing the elements as a list and on the rare occasion of a search - just do a linear search.

like image 100
amit Avatar answered Nov 15 '22 21:11

amit