Janis-Rullis-IT / pr1-symfony5-api

Dockerized Symfony 5 API
MIT License
0 stars 0 forks source link

5-Improve DB structure #22

Closed janis-rullis closed 4 years ago

janis-rullis commented 4 years ago

https://github.com/lauris-printify/homework-v3/blob/master/docker/backup/mysql/api.sql

janis-rullis commented 4 years ago

order

later #29

11 #12 #13 #14

https://github.com/janis-rullis/pr1/wiki/Uses-cases-and-Conditions

maybe

janis-rullis commented 4 years ago

product

later #30

9

janis-rullis commented 4 years ago

relation

Unsigned, indexes.

later #31

12

janis-rullis commented 4 years ago

user

later #32

8

janis-rullis commented 4 years ago

Register used queries

Selection_002

janis-rullis commented 4 years ago

user before improvements

Query

EXPLAIN SELECT t0.id AS id_1, t0.name AS name_2, t0.surname AS surname_3, t0.balance AS balance_4 FROM user t0;

image

janis-rullis commented 4 years ago

user improvements

Query

ALTER TABLE`user`
    CHANGE `balance` `balance` smallint(5) unsigned NOT NULL DEFAULT '10000' COMMENT 'All users upon creation have $100 in their balance. #8'; 

Improvement

Result

image

Endpoint

Not broken. Returns

{
  "id": 4,
  "name": "John",
  "surname": "Doe",
  "balance": 10000
}

Tests

Green.

janis-rullis commented 4 years ago

relation before improvements

View user's order

EXPLAIN
SELECT
    t0.id AS id_1,
    t0.owner_id AS owner_id_2,
    t0.order_id AS order_id_3,
    t0.product_id AS product_id_4,
    t0.quantity AS quantity_5
FROM
    relation t0
WHERE
    t0.order_id = 1 AND t0.owner_id = 1

image

janis-rullis commented 4 years ago

order before improvements

View user's all orders

SELECT
    t0.id AS id_1,
    t0.owner_id AS owner_id_2,
    t0.name AS name_3,
    t0.surname AS surname_4,
    t0.street AS street_5,
    t0.country AS country_6,
    t0.phone AS phone_7,
    t0.state AS state_8,
    t0.zip AS zip_9,
    t0.production_cost AS production_cost_10,
    t0.shipping_cost AS shipping_cost_11,
    t0.express_shipping AS express_shipping_12,
    t0.total_cost AS total_cost_13
FROM
    `order` t0
WHERE
    t0.owner_id = 1

image

janis-rullis commented 4 years ago

Relation improvements

UNSIGNED query

ALTER TABLE`relation`
      CHANGE `owner_id` `owner_id` int(11) UNSIGNED NOT NULL,
      CHANGE `order_id` `order_id` int(11) UNSIGNED NOT NULL,
      CHANGE `product_id` `product_id` int(11) UNSIGNED NOT NULL,
      CHANGE `quantity` `quantity` int(11) UNSIGNED NOT NULL

INDEX query

ALTER TABLE`relation`
      ADD INDEX `owner_order` (`owner_id`, `order_id`);

Improvements

image

image

Endpoint

Works.

Tests

Green.

janis-rullis commented 4 years ago

order improvements

Make integer field UNSIGNED because this will allow more POSITIVE values

ALTER TABLE`order`
      CHANGE `owner_id` `owner_id` int(11) UNSIGNED NOT NULL,
      CHANGE `express_shipping` `express_shipping` tinyint(1) UNSIGNED DEFAULT NULL,
      CHANGE `production_cost` `production_cost` int(11) UNSIGNED NOT NULL,
      CHANGE `shipping_cost` `shipping_cost` int(11) UNSIGNED NOT NULL,
      CHANGE `total_cost` `total_cost` int(11) UNSIGNED NOT NULL;    

Add indexes to find items by owner faster

ALTER TABLE `order`
      ADD INDEX `owner_id` (`owner_id`);

Improved

image

image

Endpoints

Tests

Green.

janis-rullis commented 4 years ago

product before improvements

Get user's products

EXPLAIN
SELECT
    t0.id AS id_1,
    t0.owner_id AS owner_id_2,
    t0.type AS type_3,
    t0.title AS title_4,
    t0.sku AS sku_5,
    t0.cost AS cost_6
FROM
    product t0
WHERE
    t0.owner_id = 1;

image

janis-rullis commented 4 years ago

product improvements

Make integer field UNSIGNED

because this will allow more POSITIVE values, also decrease the cost to SMALLINT

ALTER TABLE`product`
      CHANGE `owner_id` `owner_id` int(11) UNSIGNED NOT NULL,
      CHANGE `cost` `cost` SMALLINT(5) UNSIGNED NOT NULL COMMENT 'Limited to smallint because user cant afford nothing more than 10000 (see users init balance) #8 #9 ';

Add indexes to find user's products faster

ALTER TABLE `product`
      ADD INDEX `owner_id` (`owner_id`),
      ADD UNIQUE INDEX `sku` (`sku`);

Improvements

image

image

Endpoints

Works.

Tests

Green.