Advertisement
matteraf

Purging data when the table is big and has children tables

Mar 1st, 2023
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 3.87 KB | Software | 0 0
  1. Let’s create the schema
  2.  
  3. PS8_Primary (none)> CREATE SCHEMA IF NOT EXISTS test;
  4. Query OK, 1 row affected (0.06 sec)
  5.  
  6. PS8_Primary (none)> USE test;
  7. Database changed
  8.  
  9. Then the tables
  10.  
  11. PS8_Primary test> CREATE TABLE orders (
  12. OrderId int(11) NOT NULL AUTO_INCREMENT,
  13. Job varchar(45) NOT NULL,
  14. CustomerOrderNumber varchar(45) DEFAULT NULL,
  15. OrderDate datetime DEFAULT NULL,
  16. ProductCode varchar(45) NOT NULL,
  17. CreatedDate datetime NOT NULL,
  18. PRIMARY KEY (OrderId),
  19. KEY IX_Orders_CreatedDate (CreatedDate)
  20. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  21. Query OK, 0 rows affected, 2 warnings (0.52 sec)
  22.  
  23. PS8_Primary test> CREATE TABLE exceptions (
  24. ExceptionId int(11) NOT NULL AUTO_INCREMENT,
  25. CreatedDate datetime NOT NULL,
  26. OrderId int(11) DEFAULT NULL,
  27. Code int(11) NOT NULL,
  28. Message varchar(255) NOT NULL,
  29. PRIMARY KEY (ExceptionId),
  30. CONSTRAINT FK_Exceptions_Orders_OrderId FOREIGN KEY (OrderId) REFERENCES orders (OrderId) ON DELETE NO ACTION ON UPDATE NO ACTION
  31. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  32. Query OK, 0 rows affected, 4 warnings (0.12 sec)
  33.  
  34. PS8_Primary test> CREATE TABLE personalizations (
  35. PersonalizationId int(11) NOT NULL AUTO_INCREMENT,
  36. OrderId int(11) DEFAULT NULL,
  37. SortOrder int(11) NOT NULL,
  38. Text varchar(45) NOT NULL,
  39. PRIMARY KEY (PersonalizationId),
  40. CONSTRAINT OrderId FOREIGN KEY (OrderId) REFERENCES orders (OrderId) ON DELETE NO ACTION ON UPDATE NO ACTION
  41. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  42. Query OK, 0 rows affected, 4 warnings (0.39 sec)
  43.  
  44.  
  45. Let’s verify the child tables
  46.  
  47.  
  48. PS8_Primary test> SELECT * FROM information_schema.REFERENTIAL_CONSTRAINTS WHERE REFERENCED_TABLE_NAME IN ('orders')\G
  49. *************************** 1. row ***************************
  50.        CONSTRAINT_CATALOG: def
  51.         CONSTRAINT_SCHEMA: test
  52.           CONSTRAINT_NAME: FK_Exceptions_Orders_OrderId
  53. UNIQUE_CONSTRAINT_CATALOG: def
  54.  UNIQUE_CONSTRAINT_SCHEMA: test
  55.    UNIQUE_CONSTRAINT_NAME: PRIMARY
  56.              MATCH_OPTION: NONE
  57.               UPDATE_RULE: NO ACTION
  58.               DELETE_RULE: NO ACTION
  59.                TABLE_NAME: exceptions
  60.     REFERENCED_TABLE_NAME: orders
  61. *************************** 2. row ***************************
  62.        CONSTRAINT_CATALOG: def
  63.         CONSTRAINT_SCHEMA: test
  64.           CONSTRAINT_NAME: OrderId
  65. UNIQUE_CONSTRAINT_CATALOG: def
  66.  UNIQUE_CONSTRAINT_SCHEMA: test
  67.    UNIQUE_CONSTRAINT_NAME: PRIMARY
  68.              MATCH_OPTION: NONE
  69.               UPDATE_RULE: NO ACTION
  70.               DELETE_RULE: NO ACTION
  71.                TABLE_NAME: personalizations
  72.     REFERENCED_TABLE_NAME: orders
  73. 2 rows in set (2.42 sec)
  74.  
  75.  
  76. Have those child tables sub-child tables? NO (I checked in customer schema and they didn’t have - it’s just for information use)
  77.  
  78. PS8_Primary test> SELECT * FROM information_schema.REFERENTIAL_CONSTRAINTS WHERE REFERENCED_TABLE_NAME IN ('exceptions','personalizations');
  79. Empty set (0.03 sec)
  80.  
  81. Let’s load the tables with some data (I won’t insert millions of rows due to my laptop limitation, but it works anyway)
  82.  
  83. [percona@PS8_Primary ~]$ for ROW in {1..10000}; do echo "INSERT INTO test.orders (OrderId,Job,CustomerOrderNumber,OrderDate,ProductCode,CreatedDate) VALUES ($ROW,'OrderNumber $ROW','Job $ROW',DATE(DATE_ADD(DATE_ADD(now(), INTERVAL -365 DAY),INTERVAL $ROW HOUR)),'Product Number $ROW',DATE(DATE_ADD(DATE_ADD(now(), INTERVAL -365 DAY),INTERVAL $ROW HOUR)));"; done | mysql test -A
  84.  
  85. [percona@PS8_Primary ~]$ for ROW in {1..10000}; do for NEST in {1..2}; do echo "INSERT INTO test.exceptions (CreatedDate,OrderId,Code,Message) VALUES (DATE(DATE_ADD(DATE_ADD(now(), INTERVAL -365 DAY),INTERVAL $ROW HOUR)),$ROW,222*$ROW,'Exception Detail for order # $ROW');"; done; done | mysql test -A
  86.  
  87. [percona@PS8_Primary ~]$ for ROW in {1..10000}; do for NEST in {1..2}; do echo "INSERT INTO test.personalizations (OrderId,SortOrder,Text) VALUES ($ROW,$NEST*$ROW,'Personalization Detail for OrderID # $ROW');"; done; done | mysql test -A
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement