Skip to content

Instantly share code, notes, and snippets.

@Manzanit0
Last active August 22, 2022 09:47
Show Gist options
  • Save Manzanit0/c548569fc0794805f5ca50ba8801d521 to your computer and use it in GitHub Desktop.
Save Manzanit0/c548569fc0794805f5ca50ba8801d521 to your computer and use it in GitHub Desktop.
Random bits and bobs when learning MySQL
-- This is simply an example of how to get child records as JSON.
--
SELECT ii.id AS invoice_id,
JSON_PRETTY(JSON_OBJECT('items',
(
SELECT
CAST(CONCAT('[',
GROUP_CONCAT(JSON_OBJECT(
'id', li.id,
'name', li.name,
'service_type', li.service_type,
'discounts',
(SELECT CAST(CONCAT('[',GROUP_CONCAT(JSON_OBJECT('id', ad.id, 'name', ad.name, 'rate', ad.rate)), ']') AS JSON) FROM invoice_applied_discounts ad WHERE ad.invoice_line_item_id = li.id)
)),
']') AS JSON)
FROM invoice_line_items li
WHERE li.invoice_id = ii.id
)
)) AS line_items
FROM invoices ii
WHERE ii.id = '2DUJV2VlVHXGT7BGKcGAQ7Daplu'\G
-------------------------------------------
------------- EXAMPLE RESULT --------------
-------------------------------------------
-- invoice_id | 2DUJV2VlVHXGT7BGKcGAQ7Daplu
-- line_items | {
-- "items": [
-- {
-- "id": 1672655,
-- "name": "1% Mem",
-- "discounts": [
-- {
-- "id": 17050,
-- "name": "5% Discount",
-- "rate": 5000
-- }
-- ],
-- "service_type": "memberships"
-- },
-- {
-- "id": 1672656,
-- "name": "Access Card",
-- "discounts": [
-- {
-- "id": 17051,
-- "name": "Half off",
-- "rate": 50000
-- }
-- ],
-- "service_type": "products"
-- }
-- ]
-- }

Random bits and bobs when learning MySQL

ALTER TABLE DLL

Temporary tables are usually spun up

Storage, Performance, and Concurrency Considerations

In most cases, ALTER TABLE makes a temporary copy of the original table. MySQL waits for other operations that are modifying the table, then proceeds. It incorporates the alteration into the copy, deletes the original table, and renames the new one. While ALTER TABLE is executing, the original table is readable by other sessions. Updates and writes to the table that begin after the ALTER TABLE operation begins are stalled until the new table is ready, then are automatically redirected to the new table without any failed updates. The temporary table is created in the database directory of the new table. This can differ from the database directory of the original table for ALTER TABLE operations that rename the table to a different database.

ALGORTHIM=COPY blocks the table

An ALTER TABLE operation that uses the COPY algorithm prevents concurrent DML operations. Concurrent queries are still allowed. That is, a table-copying operation always includes at least the concurrency restrictions of LOCK=SHARED (allow queries but not DML). You can further restrict concurrency for operations that support the LOCK clause by specifying LOCK=EXCLUSIVE, which prevents DML and queries.

Cheatsheet

Getting the size of the databases in a server in MB:

SELECT table_schema, sum((data_length+index_length)/1024/1024) AS MB
FROM information_schema.tables
GROUP BY 1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment