0

Is it possible to show current MySQL query progress with AJAX? I have simple AJAX request that get data from a MySQL database but sometimes it take a while like 5 seconds (it depends on row count).

Here is the code:

$.ajax({
        xhr: function()
                  {
                    var xhr = new window.XMLHttpRequest();

                    //Download progress
                    xhr.addEventListener("progress", function(evt){

                         var percentComplete = evt.loaded / evt.total;

                        console.log(percentComplete);

                    }, false);
                    return xhr;
                  },

        url: '/main/pw_RedrawGraph/',
        type: 'POST',
        dataType: 'json',
        data: {
            date_from: date_from,
            date_to:date_to
        },
        success:function(data){
            console.log(data)
        }
    });

As you see I have tried do it with xhr progress but not working because evt.loaded / evt.total is infinite

Any idea how to resolve that problem?

Anders
  • 8,307
  • 9
  • 56
  • 88
user1559599
  • 495
  • 1
  • 5
  • 17
  • Is the query done from PHP? Also, what is it that takes time - actually querying the database, or doing something with the result (like formatting it as HTML)? I think to solve this you will need to implement it in the PHP. – Anders Oct 01 '15 at 13:40
  • This might be of interest: http://stackoverflow.com/questions/15298071/progress-bar-with-mysql-query-with-php – Anders Oct 01 '15 at 13:41
  • Also, this: http://stackoverflow.com/questions/5464931/mysql-long-query-progress-monitoring – Anders Oct 01 '15 at 13:42
  • Yes it is done with php, and it takes 5 secs only query there is sometimes 200k rows. I'm using codeignieter. Chart is redraw with AJAX so i want to make percentage progressbar of this query. – user1559599 Oct 01 '15 at 13:43
  • If it is the query (not looping through the result) that takes time, I think your only possibility is to divide it into sub queries (using `LIMIT`) and monitoring how many of those that has finished. So quite a hazzle in other words. – Anders Oct 01 '15 at 13:44

1 Answers1

0

Is this for you, or a user?

If you have a low variance in execution times.. I'd be tempted to guess how long it takes, either by benchmarking a few times or by storing a rolling average of the time it takes to run.

I'd then build a progress bar that fills up at a slightly slower than appropriate speed so that it rarely fills.

Failing that I'd just use a spinner.

Arth
  • 12,789
  • 5
  • 37
  • 69