Question

I have a PHP application retrieving data from a MySQL DB. When PHP queries the DB, results are pulled and all the output rows are printed to the screen, but at the top a graph is also printed giving a visualisation of the output rows. As per the following (this is a 24 vertical line bar chart, because MySQL produced 24 rows of output from the query executed on behalf of PHP);

24 rows of MySQL output

The problem I face arises if the user runs a query that produces lots of results. The second picture further down is a visual representation of the 150 results MySQL produced when queried on behalf of PHP using different selectable parameters, in the PHP application. I would like have a max number of vertical lines (lets for example sake say 50). When a query produces more than 50 rows we consolidate the MySQL results into just 50 rows for visualisation. I will still be printing all rows below the graph so the user can look at any one result if they so wish. My question is, What is the optimal way to do this with regard to CPU cycles, without being too difficult to understand and follow in the code?

150 rows of MySQL output

One idea I have had is to tackle this in the PHP application, where I simply divide the number of rows of MySQL output by 50 (again, lets assume easy numbers, rounding is a simple issue to work with in this instance). If MySQL produces 500 rows, 500 divided by 50 is 10, so I use every 10th row to draw a vertical line on the bar chart.

Another idea is to tackle this problem in MySQL. I could perhaps run the MySQL query, then copy the results to a variable, then run a 2nd query against that which selects either every 10th row, or if there is some way to consolidate within MySQL, and then present a 2nd, smaller data set to PHP.

Where would be the best place to tackle this issue, and why, and how?

Update:

As requested to explain a little further, Its a quite basic scenario really:

Results looklike the below, each row is an integer value and a time stamp (the bar chart shows value [Y axis] over time period [X axis]);

row | value | timestamp 
 1  | 10    | 1234-01-01 10:30
 2  | 10    | 1234-01-01 10:35
 3  | 15    | 1234-01-01 10:40
 4  | 15    | 1234-01-01 10:45
 5  | 10    | 1234-01-01 10:50

When MySQL produces 150 rows of output we get a result like the 2nd graph above. I am trying to create a consolidation feature because if the query produces 300 results or example, the lines in the bar chart are all but invisible because they are so thin. That becomes an unnecessary level of visual granularity.

Was it helpful?

Solution

I have solved this issue my self in the PHP application quite simply, rather than making SQL do the work.

In the application I have an array that contains all the MySQL result rows. PHP loops through each Array entry and echo's out the Javascript to display it as a vertical bar on the bar chart.

Inserting this code before that loop solved the issue in a simple and enough manner;

$maxbars = 50;
if(count($array_mysql_resutls)>$maxbars) {
    $devision = count($array_mysql_resutls)/$maxbars;
    $resultsarray = Array();
    for($i = 0; $i < $maxbars; $i++) {
        $resultsarray[$i] = $array_mysql_resutls[ceil($devision*$i)];
    }
    $graph_traces = $resultsarray;
}

Most importantly, we are rounding up the divided figure (ceil()) for an even spread across the results set.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top