Advertisement
vvccs

ADS_3_PARTIONING

Oct 15th, 2024 (edited)
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 1.70 KB | None | 0 0
  1. Mysql
  2. use db;
  3. CREATE TABLE employees (
  4.     emp_id INT,
  5.     emp_name VARCHAR(20),
  6.     age INT,
  7.     city CHAR(25),
  8.     salary DECIMAL(18,2)
  9. ) PARTITION BY RANGE (age) (
  10.     PARTITION emp_p1 VALUES LESS THAN (20),
  11.     PARTITION emp_p2 VALUES LESS THAN (30),
  12.     PARTITION emp_p3 VALUES LESS THAN (40),
  13.     PARTITION emp_p4 VALUES LESS THAN (50),
  14.     PARTITION emp_p5 VALUES LESS THAN (MAXVALUE)  -- Catch-all partition
  15. );
  16.  
  17. drop table employees
  18.  
  19. INSERT INTO employees VALUES
  20. (1, 'John', 19, 'New York', 1200.00),
  21. (2, 'David', 49, 'London', 3200.00),
  22. (3, 'Alice', 25, 'Paris', 4500.00),
  23. (4, 'Emma', 35, 'Berlin', 2100.00),
  24. (5, 'Bob', 15, 'San Francisco', 1500.00),  -- Will go to emp_p1
  25. (6, 'Charlie', 29, 'Boston', 2800.00);     -- Will go to emp_p2
  26.  
  27. -- Select all records from the employees table
  28. SELECT * FROM employees;
  29.  
  30. SELECT partition_name, table_rows
  31. FROM information_schema.partitions
  32. WHERE table_name = 'employees';
  33. -- Select all records from partition emp_p1
  34. SELECT * FROM employees PARTITION (emp_p1);
  35.  
  36. --VERTICAL PARTIONING
  37. CREATE TABLE INVENTORY (
  38.     id INT,
  39.     product_name VARCHAR(50),
  40.     product_quantity INT,
  41.     product_price INT
  42. ) PARTITION BY RANGE COLUMNS(product_quantity, product_price) (
  43.     PARTITION P_low_stock VALUES LESS THAN (10, 100),
  44.     PARTITION P_medium_stock VALUES LESS THAN (50, 500),
  45.     PARTITION P_high_stock VALUES LESS THAN (200, 1200)
  46. );
  47. INSERT INTO INVENTORY VALUES
  48. (1, 'Headphones', 5, 50),
  49. (2, 'Mouse', 15, 200),
  50. (3, 'Monitor', 30, 300),
  51. (4, 'Keyboard', 60, 600),
  52. (5, 'CPU', 100, 1000);
  53. SELECT PARTITION_NAME, TABLE_ROWS
  54. FROM INFORMATION_SCHEMA.PARTITIONS
  55. WHERE TABLE_NAME='INVENTORY';
  56. SELECT * FROM INVENTORY PARTITION (P_high_stock);
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement