Code Monkey home page Code Monkey logo

database-task's Introduction

Task:

There is a process that generates statistics on transactions for different periods:

  • 24 hours
  • Weekly
  • Monthly
  • Quarterly
  • Year

The query looks like this:

SELECT
  DATE_FORMAT(CreatedTs, "%Y-%m-%d") `period`,
  StatusId,
  COUNT(idDeal) `count`
FROM Deals
WHERE CreatedTs BETWEEN "2021-01-01 00:00:00" AND "2021-01-31 23:59:59"
GROUP BY 1,
         2;

For a while, this query ran quite fast, but as the project evolved, the amount of data in the database increased significantly, which led to a decrease in its performance. What actions should be taken to improve query performance?

Solution

Step Zero

Modified column and table names to better fit Laravel conventions

  • CreatedTs -> created_at
  • period -> created_date
  • StatusId -> status_id
  • idDeal -> id
  • Deals -> deals

and year is set to 2023 instead of 2021

SELECT
  DATE_FORMAT(created_at, "%Y-%m-%d") `created_date`,
  status_id,
  COUNT(id) `count`
FROM deals
WHERE created_at BETWEEN "2023-01-01 00:00:00" AND "2023-01-31 23:59:59"
GROUP BY 1,
         2;

Step One

Replace COUNT(id) with COUNT(*)

SELECT
  DATE_FORMAT(created_at, "%Y-%m-%d") `created_date`,
  status_id,
  COUNT(*) `count`
FROM deals
WHERE created_at BETWEEN "2023-01-01 00:00:00" AND "2023-01-31 23:59:59"
GROUP BY 1,
         2;

Step Two

use DATE instead DATE_FORMAT

SELECT
  DATE(created_at) `created_date`,
  status_id,
  COUNT(id) `count`
FROM deals
WHERE created_at BETWEEN "2023-01-01 00:00:00" AND "2023-01-31 23:59:59"
GROUP BY 1,
         2;

Step Three

add created_date generated column

ALTER TABLE deals ADD COLUMN created_date DATE GENERATED ALWAYS AS (DATE(created_at)) STORED;
SELECT
  created_date,
  status_id,
  COUNT(id) `count`
FROM deals
WHERE created_date BETWEEN "2023-01-01" AND "2023-01-31"
GROUP BY created_date, status_id;

Step Four

add composite index key on created_date and status_id

ALTER TABLE deals ADD INDEX created_date_status_id_index (created_date, status_id)

Benchmarks

Setup

  • install dependencies with comporser install
  • set your database connection on .env file
  • import database/dump.sql or (php artisan migrate && php artisan db:seed)
  • run benchmarks with
php artisan app:bench

database-task's People

Contributors

jepp112 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.