A New Sorting Algorithm for Accelerating Join-based Queries

The performance of several Database Management Systems (DBMSs) and Data Stream Management Systems (DSMSs) queries is dominated by the cost of the sorting algorithm. Sorting is an integral component of most database management systems. Stable sorting algorithms play an important role in DBMS queries since such operations requires stable sorting outputs. In this paper, we present a new stable sorting algorithm for internal sorting that scans an unsorted input array of length n and arranges it into m sorted sub-arrays. By using the m-way merge algorithm, the sorted m sub-arrays will be merged into the final output sorted array. The proposed algorithm keeps the stability of the keys intact. The scanning process requires linear time complexity (O(n)) in the best case, and O(n log m) in the worst case, and the m-way merge process requires O (n log m) time complexity. The proposed algorithm has a time complexity of O (n log m) element comparisons. The performed experimental results have shown that the proposed algorithm outperforms other stable sorting algorithms that are designed for join-based queries.


INTRODUCTION
A stable sort is a sorting algorithm which when applied to records with same key values, they retain their order.In other words, for two records with the same key value, the outcome of the sorting algorithm will have the records in the same order, although their positions relative to other records may change [1,2,3].Stability of a sorting algorithm is a property of the algorithm, not of the comparison mechanism [4,5,6,7].For instance, quick Sorting algorithm is not stable while Merge Sort algorithm is stable.
Sorting is an integral component of most database management systems (DBMSs) [8,2,9].Sorting can be both computation-intensive as well as memory intensive [10,7,11,12].The performance of DBMS queries is often dominated by the cost of the sorting algorithm.Most DBMS queries require sorting with stable results [4,10,13].
External memory sorting algorithms reorganize large datasets.They typically perform two phases [4,14,15,16].The first phase produces a set of ordered sub-files, while the second phase processes these sub-files to produce a totally ordered output data file.A popular and important class of the external memory sorting algorithms is the Merge-Based Sorting algorithm, where input data is partitioned into data chunks of approximately equal size, sorts these data chunks in main memory and writes the "runs" to disk.The second phase merges the runs in main memory and writes the sorted output to the disk.Our proposed algorithm is inspired by the Merge-Based Sorting algorithm.In order to sort n elements placed in memory array A, the proposed algorithm divides the n elements of A into m ordered sub-arrays, each with length l; l= n/m.Initially, all sub-arrays are empty, then elements of array A are scanned and inserted into the ordered sub-arrays based on the fullness of sub-arrays, and whether the inserted element is greater than the last element of the sub-arrays or not.If all sub-arrays do not satisfy the insertion criteria, the scanned element is inserted in place into a temporary sub-array, temp, with length l.If the temporary sub-array is full, all sub-arrays and sub-array temp are merged.The merged elements are placed into the sub-arrays by fully filling them in order.The process continues with the elements left in array A. After scanning all elements of A, the produced m sub-arrays are merged to form the final sorted list.
External memory sorting performance is often limited by I/O performance [17,13,18].Disk I/O bandwidth is significantly lower than main memory bandwidth.Therefore, it is important to minimize the amount of data written to and read from disks.Large files will not fit in RAM so we must sort the data in at least two passes.Each pass reads and writes to the disk.CPU-based sorting algorithms incur significant cache misses on data sets that do not fit in the data caches [19,18].Therefore, it is not efficient to sort partitions comparable to the size of main memory [20,16,21].These results in a tradeoff between disk I/O performance and CPU computation time spent in sorting the partitions.For example, in merge-based external sorting algorithms, the time spent in Phase 1 can be reduced by choosing run sizes comparable to the CPU cache sizes.However, this choice increases the time spent in Phase 2 to merge a large number of small runs.
In this paper, a new stable sorting algorithm that reduces the number of comparisons, element moves, and required auxiliary storage will be presented.Our algorithm operates when used as an internal sorter with an average m log m element comparisons, m log m element moves, and exactly n auxiliary storage, where n is the size of the input array to be sorted, and m is the average size of sub-arrays.No auxiliary arithmetic operations with indices will be needed.The proposed algorithm has been tested in extensive performance studies.Experimental results have shown that when the proposed algorithm is adapted to sort externally, it accelerates the computation of equijoin and non-equi-join queries in databases, and numerical statistic queries on data streams.The benchmarks used in the performance studies are databases consisting of up to one million values.
In the following sections, the proposed algorithm, and the computational requirement analysis will be presented in details.The proposed algorithm and the proof of its stability are presented in section 2, and the m -way-merge algorithm is discussed in section 3. The analysis of time complexity and the analysis of the external version of the proposed algorithm are discussed in section 4. Experimental results are presented in section 5. Section 6 concludes the paper.

THE PROPOSED STABLE SORTING ALGORITHM
There are some issues that have to be considered to guarantee stability.First, the input array has to be scanned in sequential order from element a 1 to element a n .In order to determine where to place a specific element in a segment, the element should be placed in the first segment that meets the condition that this element is greater than or equal to the last element in the sub-array.Also, some issues have to be considered in merging.The m-way-merge operation that produces the output list, should select values from segments 1 , 2 , …, m in preference according to the segment order.This will be discussed further in the following section.
The main idea of the proposed algorithm is to divide the elements a i , i=1,…, n of the input array A with length n into some disjoint segments σ 0 , σ 1 , . . ., σ m , of equal lengths l; l= n/m.All elements in segments σ i 's, i=1,…, m, are sorted according to key k.The algorithm starts with empty segments σ 0 , σ 1 , . . ., σ m , i.e., for each segment σ i , i=1,…, m, the pointer to its last element last i =0. a i is compared to the last element of the not full segments σ j , j=1,…, m. a i should fall in one of the following two cases: • a i ≥ σ j (last j ), where j=1,…, m: a i is appended to σ j .• a i < σ j (last j ) for all j with last j ≠l: insert a i in σ temp .
If the temporary segment σ temp is full, then the m segments are m-way-merged, the 2-way-merged with the temporary segment σ temp .Assume the number of sorted elements is L. The L elements are divided orderly on the first S segments; S= ; such that the first S-1 segments are full.The first S-1 segments will not be included in the subsequent inserts.The algorithm will consider only those buffers from S to m.
In the merging process, we should consider the segments order.When two segments and , k 1 <k 2 are merged and if there exists two elements in and in , such that , then in the merged segment, is placed before .The final sorted array is obtained by using an m-way merging algorithm.The new proposed algorithm is given below.The following lemma states that the Stable-Sorting algorithm is stable.Lemma: Let A be an array with n elements.The Stable Sort algorithm guarantees that the resulted sorted array is stable.Following the steps of the Stable Sort algorithm, the proof of the above lemma is obvious.

THE MERGING
In this section, the m -way-merge algorithm is going to be discussed.2-waymerge is discussed first to show complexity measures and stability issues of the algorithm.The discussion is then generalized to the proposed more general case of mway-merge.
Merging is the process whereby two sorted lists of elements are combined to create a single sorted list.Originally, most sorting was done on large database systems using a technique known as merging.Merging is used because it easily allows a compromise between expensive internal Random Access Memory (RAM) and external magnetic mediums where the cost of RAM is an important factor.Originally, merging is an external method for combining two or more sorted lists to create a single sorted list on an external medium.
The most natural way to merge two lists A with n 1 elements and B with n 2 elements of presorted data values is to compare the values at the lowest (smallest) locations in both lists and then output the smaller of the two.The next two smallest values are compared in a similar manner and the smaller value output.This process is repeated until one of the lists is exhausted.Merging in this way requires n 1 + n 2 -1 comparisons and n 1 +n 2 data moves to create a sorted list of n 1 +n 2 elements.The order in which each value in the A and B list is selected for output to the C list is labeled by their subscript in the second listing of A and B. Merging in this manner is easily made stable.In addition, this lower limit is guaranteed regardless of the nature of the input permutation.
A merge algorithm is stable if it always leaves sequences of equal data values in the same order at the end of merging.
Example: Let A and B be two sorted lists with the values given in Figure 1.a.The 2-way merge algorithm merges lists A and B into list C as given in Figure 1.b.If the merge algorithm is merging two lists such that it keeps the indices of a sequence of equal values from a given list in sorted order, i.e. they are kept in the same order as they were before the merge as illustrated in the example 1, the merge algorithm is said to be stable.Otherwise, the algorithm is said to be unstable.
Stability is defined such that values from A are always given preference whenever a tie occurs between values in A and B, and that the indices of equal set of values are kept in sorted order at the end of the merge operation.The proposed m-waymerge operation can be implemented as a series of 2-way merge operations with preserving stability requirements, or can be merge m lists in the same time.The 2-waymerge operation can be generalized for m sorted lists, where m is some positive integer > 2. For keeping track of the next smallest value in each list during the merge operation, m pointers will be needed.The number of comparisons needed for merging m sorted lists, each consisting of l data items is O(n log 2 (m)), where n = ml.

Time Complexity
Scanning the elements of the input array of n elements and divide it into m sorted sub-arrays is done in linear time, except when the temporary array reaches its maximum length.The best case, when the temporary array is not reaching its maximum length, needs O(n) comparison and O(n) moves.The maximum time required for doing the sorting process, and keeping the stability required, needs O(n log m) comparison and O(n) moves.In the worst case, each of the empty buffers will hold only one element, and the algorithm may have to insert elements to the temporary array until it gets full.The process is repeated until all elements are inserted.Below, we prove that the Stable-Sorting algorithm has a time complexity of O (n log m), where m could be much smaller than n.
Assume n=m r , for some integer r > 0, if r=1, which means number of buffers m equals to number of records n, the proposed approach could have, in the worst case, O(n log n) time complexity.If the number of buffers is decreased, by increasing r, we may get less cost but to a certain limit.If ; , ) the proposed approach could have O(n log n) cost.From fig. 2, 3, and 4, we conclude that the best value for r is 4.
Best Case: <= n+ n log(m)+l log(l) O(n log (m)) Worst Case: 1 : m getting m elements into m buffers : l log(l) getting l elements (sorted) into temp buffer : m log(m) m-way merge of m buffers : (m+l) merge with temp buffer : (m+ l) placing l elements in buffer 1 and m elements in buffer 2 : Total = m +l log(l) + m log(m) + 2 (m+l) In general in iteration i i : m-i getting m-i into (m-i) buffers : l log(l) getting l elements (sorted) into temp buffer : <(im-i)log(m) (m-i)-way merge of m-i buffers < im log (m-i) : (im-i+ l) merge with temp buffer : (im-i+ l) placing l elements in buffer m-i and im-x(i) elements in buffer i : Total = (m-i) +l log(l) + (im-i)/2) log(m) + 2(im-i+l) The process will be repeated m times Total cost or Total cost In case that n=m 3 , then l=m 2   Total cost Total cost is O(n log m) if n=m 4 , l=m 3   Total cost Total cost is O(n log m) In general, if n=m r , l=m r-1 Total cost If r=1; m=n, the proposed approach could have O(n log n) cost.If we decrease the number of buffers, by increasing r, we may get less cost but to a certain limit.Part of the total cost equation, ( ), will increase the total cost.If ; , ) the proposed approach could have O(n log n) cost.From Figures 2, 3, and 4, the best value of r is 4.

Space Complexity
There is extra auxiliary storage equal to an array of the same size as the input array.Clever implementation of the proposed Stable-Sorting algorithm can decrease the required auxiliary storage.There are no indices or any other hidden data structures required for the execution of the algorithm.Auxiliary storage that is required for the proposed algorithm is O(n).

External Sorting Version of The Proposed Algorithm
In the proposed implementation of our algorithm, the input file is sorted in two passes.Each pass reads and writes to the disk.The proposed algorithm is adapted to perform external sorting using run sizes comparable to the available RAM size to minimize the time required for the second part of the algorithm which is the final merging operation.This is done by assuming the input array will be sorted in a file on the hard disk called f.The file f is much larger than the available space in main memory.For an available main memory of g bytes, we divide the space available in the main memory into two partitions, g 1 and g 2 ; g=g 1 +g 2 .The first g 1 bytes of the file f are copied into the first partition.The other g 2 bytes of the main memory are divided into m+1 blocks each has length l; g 2 =(m+1)l and g 1 =ml.We perform the proposed algorithm on the portion of f in the main memory.The g 1 bytes of the sorted output will be stored back in file f in place of its first g 1 bytes.Then we repeat the same procedure on the next g 1 bytes from file f until the whole file is exhausted.The second part is to mway-merge, the sorted g 1 chunks of file f.The merge procedure is performed externally.

EXPERIMENTAL RESULTS
Performance study is being carried on our proposed algorithm as an internal sorting algorithm and its performance is compared to other well known internal sorting algorithms.These sorting algorithms are Merge-sort, K-way merge sort and quick sort algorithms.The performance improvement obtained by using our proposed algorithm with m=4, against mergesort (Figure 5.a) is gaining on the average a 45% speedup over mergesort optimized CPU implementation.With m=8, our proposed algorithm on average is 40% faster than k-way mergesort optimized CPU implementation (Figure 5.b).
The performance improvement obtained by using our proposed algorithm with m=4, against Quick sort (Figure 5.c) is a gaining of 35% on the average over Quick sort optimized CPU implementation, and finally, the performance improvement obtained using our proposed algorithm against Quick sort (Figure 5.d) is gaining on the average a 20% speedup over Quick sort optimized CPU implementation.Figure 5.d depicts the running times of sorting algorithms in milliseconds for a number of keys to be sorted ranging from 1000 to 8000 keys each key is 20 characters long.

CONCLUSIONS
In this paper, a new sorting algorithm was presented.This algorithm is stable sorting algorithm with O(n log m), comparisons where m is much smaller than n, and O(n) moves and O(n) auxiliary storage.Comparison of the new proposed algorithm and some well-known algorithms has proven that the new algorithm outperforms the other algorithms.Further, there is no auxiliary arithmetic operations with indices required.Besides, this algorithm was shown to be of practical interest, in join-based queries.We have improved the performance of join operations by applying our fast sorting algorithm and compared its performance against other optimized nested join algorithms.Our algorithm also has low bandwidth requirements 7.

Figure 1 Figure 1 .
Figure 1.a Figure 1.b Figure 1.The 2-way merging of A and B