Commit 32a33321 authored by Erick Hitter's avatar Erick Hitter
Browse files

Allow sorting to be enforced when listing events, but default to unsorted

We generally don't need sorted events, making the filesort a waste of time and resources.
parent d9021f6e
......@@ -374,8 +374,15 @@ class Events_Store extends Singleton {
$offset = 0;
}
// Do not sort, otherwise index isn't used
$jobs = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$this->get_table_name()} WHERE status = %s LIMIT %d,%d;", $args['status'], $offset, $args['quantity'] ), 'OBJECT' );
// Avoid sorting whenever possible, otherwise filesort is used
// Generally only necessary in CLI commands for pagination, as full list of events is usually required
if ( isset( $args['force_sort'] ) && true === $args['force_sort'] ) {
$query = $wpdb->prepare( "SELECT * FROM {$this->get_table_name()} WHERE status = %s ORDER BY timestamp ASC LIMIT %d,%d;", $args['status'], $offset, $args['quantity'] );
} else {
$query = $wpdb->prepare( "SELECT * FROM {$this->get_table_name()} WHERE status = %s LIMIT %d,%d;", $args['status'], $offset, $args['quantity'] );
}
$jobs = $wpdb->get_results( $query, 'OBJECT' );
if ( is_array( $jobs ) ) {
$jobs = array_map( array( $this, 'format_job' ), $jobs );
......
......@@ -194,6 +194,7 @@ class Events extends \WP_CLI_Command {
'status' => $event_status,
'quantity' => $limit,
'page' => $page,
'force_sort' => true,
) );
// Bail if we don't get results
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment