depart_table.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. const BaseUtil = require('./baseUtils');
  2. const ledgerTables = ['zh_ledger', 'zh_ledger_extra', 'zh_revise_bills', 'zh_stage_bills', 'zh_stage_bills_final', 'zh_settle_bills'];
  3. const ledgerDepartKey = ['tender_id', 'tid', 'tender_id', 'tid', 'tid', 'tid'];
  4. const posTables = ['zh_pos', 'zh_pos_extra', 'zh_revise_pos', 'zh_stage_pos', 'zh_stage_pos_final', 'zh_settle_pos'];
  5. const posDepartKey = ['tid', 'tid', 'tid', 'tid', 'tid', 'tid'];
  6. const orgLedgerDepart = 10, orgPosDepart = 20;
  7. const tableDepart = 1000;
  8. const sqlArr = [];
  9. const getDepartSql = function (table, key, orgDepart, newDepart) {
  10. sqlArr.push(`-- ${table}`);
  11. sqlArr.push(`-- ${table} RENAME`);
  12. for (let i = 0; i < orgDepart; i++) {
  13. sqlArr.push(`RENAME TABLE ${table}_${i} TO ${table}_bak_${i};`);
  14. }
  15. sqlArr.push(`-- ${table} RE_DEPART`);
  16. for (let i = 0; i < newDepart; i++) {
  17. const orgDepartIndex = i % orgDepart;
  18. sqlArr.push(`Create Table ${table}_${i} SELECT * FROM ${table}_bak_${orgDepartIndex} WHERE ${key} % 100 = ${i};`);
  19. }
  20. sqlArr.push('');
  21. };
  22. const getArrDepartSql = function(tableArr, keyArr, orgDepart, newDepart) {
  23. for (let i = 0, iLen = tableArr.length; i < iLen; i++) {
  24. getDepartSql(tableArr[i], keyArr[i], orgDepart, newDepart);
  25. }
  26. };
  27. getArrDepartSql(ledgerTables, ledgerDepartKey, orgLedgerDepart, tableDepart);
  28. getArrDepartSql(posTables, posDepartKey, orgPosDepart, tableDepart);
  29. BaseUtil.saveBufferFile(sqlArr.join('\n'), BaseUtil.getFileName('depart_table.sql'));
  30. Select Sum(amount) FROM (
  31. Select Count(*) As amount From zh_settle_bills_bak_0
  32. UNION ALL
  33. Select Count(*) As amount From zh_settle_bills_bak_1
  34. UNION ALL
  35. Select Count(*) As amount From zh_settle_bills_bak_2
  36. UNION ALL
  37. Select Count(*) As amount From zh_settle_bills_bak_3
  38. UNION ALL
  39. Select Count(*) As amount From zh_settle_bills_bak_4
  40. UNION ALL
  41. Select Count(*) As amount From zh_settle_bills_bak_5
  42. UNION ALL
  43. Select Count(*) As amount From zh_settle_bills_bak_6
  44. UNION ALL
  45. Select Count(*) As amount From zh_settle_bills_bak_7
  46. UNION ALL
  47. Select Count(*) As amount From zh_settle_bills_bak_8
  48. UNION ALL
  49. Select Count(*) As amount From zh_settle_bills_bak_9
  50. ) As temp;
  51. // 数据量预估
  52. // qa执行时间(分表100)
  53. // zh_ledger: 320s(876461条)
  54. // zh_ledger_extra: 80s(155730条)
  55. // zh_revise_bills: 230s(386041条)
  56. // zh_stage_bills: 90s(80435条)
  57. // zh_stage_bills_final: 90s(108374条)
  58. // zh_settle_bills: 40s(360条)
  59. // zh_pos: 480s(3122249条)
  60. // zh_pos_extra: 50s(77条)
  61. // zh_revise_pos: 350s(1173963条)
  62. // zh_stage_pos: 110s(380576条)
  63. // zh_stage_pos_final: 130s(658929条)
  64. // zh_settle_pos: 50s(266条)