Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting inside the database or Sorting in code behind? Which is best?

I have a dropdown list in my aspx page. Dropdown list's datasource is a datatable. Backend is MySQL and records get to the datatable by using a stored procedure.

I want to display records in the dropdown menu in ascending order.

I can achieve this by two ways.

1) dt is datatable and I am using dataview to filter records.

dt = objTest_BLL.Get_Names();

dataView = dt.DefaultView; 
dataView.Sort = "name ASC";
dt = dataView.ToTable();

ddown.DataSource = dt;
ddown.DataTextField = dt.Columns[1].ToString();
ddown.DataValueField = dt.Columns[0].ToString();
ddown.DataBind();

2) Or in the select query I can simply say that

SELECT
`id`,
`name`
FROM `test`.`type_names`
ORDER BY `name` ASC ;

If I use 2nd method I can simply eliminate the dataview part. Assume this type_names table has 50 records. And my page is view by 100,000 users at a minute. What is the best method by considering efficiency,Memory handling? Get unsorted records to datatable and filter in code behind or sort them inside the datatabse?

like image 599
Prageeth Liyanage Avatar asked Nov 07 '14 03:11

Prageeth Liyanage


People also ask

What is the sorting of database?

Sorting is the process of arranging data into meaningful order so that you can analyze it more effectively. For example, you might want to order sales data by calendar month so that you can produce a graph of sales performance. You can use Discoverer to sort data as follows: sort text data into alphabetical order.

How do I sort in SQL code?

The ORDER BY keyword is used to sort the result-set in ascending or descending order. The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.

Which clause is used to sort the data?

The SQL ORDER BY clause is used to sort the data in ascending or descending order, based on one or more columns.

How do you sort a table in Java?

To sort one of the columns in descending order, simply preface the column name with a minus sign '-', as in: table. sortOn("-recipe","y", "mix"); This sorts the table first on recipe in descending order, then on y and mix, in ascending order.


1 Answers

Note - Only real performance tests can tell you real numbers.. Theoretical options are below (which is why I use word guess a lot in this answer). You have at least 3 (instead of 2) options -

  1. Sort in database - If the column being sorted on is indexed.. Then this may make most sense, because overhead of sorting on your database server may be negligible. SQL servers own data caches may make this super fast operation.. but 100k queries per minute.. measure if SQL gives noticeably faster results without sort.

  2. Sort in code behind / middle layer - Likely you won't have your own equivalent of index.. you'd be sorting list of 50 records, 100k times per minutes.. would be slower than SQL, I would guess. Big benefit would apply, only if data is relatively static, or very slow changing, and sorted values can be cached in memory for few seconds to minutes or hours..

  3. The option not in your list - send the data unsorted all the way to the client, and sort it on client side using javascript. This solution may scale the most... sorting 50 records in Browser, should not be a noticeable impact on your UX.

like image 127
Vikas Gupta Avatar answered Nov 15 '22 22:11

Vikas Gupta