sch_aus_cleanup.sql 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700
  1. {**************************************************************************}
  2. {* *}
  3. {* Licensed Materials - Property of IBM and/or HCL *}
  4. {* *}
  5. {* IBM Informix Dynamic Server *}
  6. {* (c) Copyright IBM Corporation 1996, 2009 All rights reserved. *}
  7. {* (c) Copyright HCL Technologies Ltd. 2017. All Rights Reserved. *}
  8. {* *}
  9. {**************************************************************************}
  10. {*********************************************************
  11. *
  12. * This sql file should be run against sysadmin database
  13. * to cleanup and reinstall the AUS feature
  14. *
  15. ********************************************************}
  16. {*********************************************************
  17. *
  18. * The following section cleans up all the resources
  19. * utilized by the AUS feature
  20. *
  21. ********************************************************}
  22. {*********************************************************
  23. * Allow us to delete these tasks and disable them so they do
  24. * not try and run while we are doing maintenance
  25. ********************************************************}
  26. UPDATE ph_task SET(tk_attributes,tk_enable)=(0,'f') WHERE
  27. tk_name MATCHES "Auto Update Statistics*";
  28. {*********************************************************
  29. * Drop all AUS function/SPL
  30. ********************************************************}
  31. DROP FUNCTION IF EXISTS aus_get_realtime();
  32. DROP FUNCTION IF EXISTS aus_refresh_stats(INTEGER, INTEGER, INTEGER);
  33. DROP FUNCTION IF EXISTS aus_refresh_stats();
  34. DROP FUNCTION IF EXISTS aus_refresh_stats(INTEGER, INTEGER);
  35. DROP FUNCTION IF EXISTS aus_refresh_stats_orig();
  36. DROP FUNCTION IF EXISTS aus_refresh_stats_orig(INTEGER, INTEGER);
  37. DROP FUNCTION IF EXISTS aus_refresh_upgrade();
  38. DROP FUNCTION IF EXISTS aus_refresh_downgrade();
  39. DROP FUNCTION IF EXISTS aus_evaluate_stats(INTEGER, INTEGER);
  40. DROP FUNCTION IF EXISTS aus_evaluator_upgrade();
  41. DROP FUNCTION IF EXISTS aus_evaluator_downgrade();
  42. DROP FUNCTION IF EXISTS aus_setup_mon_table_profile(INTEGER, INTEGER);
  43. DROP FUNCTION IF EXISTS aus_setup_mon_table_profile(INTEGER, INTEGER, INTEGER);
  44. DROP FUNCTION IF EXISTS aus_cleanup_table(INTEGER);
  45. DROP FUNCTION IF EXISTS aus_setup_table();
  46. DROP FUNCTION IF EXISTS aus_setup_table(INTEGER);
  47. DROP FUNCTION IF EXISTS aus_enable_refresh();
  48. DROP FUNCTION IF EXISTS aus_evaluator(BOOLEAN);
  49. DROP FUNCTION IF EXISTS aus_evaluator(INTEGER,INTEGER);
  50. DROP FUNCTION IF EXISTS aus_evaluator(INTEGER,INTEGER,INTEGER);
  51. DROP FUNCTION IF EXISTS aus_evaluator_dbs(char(128),INTEGER, INTEGER);
  52. DROP FUNCTION IF EXISTS aus_evaluator_dbs(char(128),INTEGER, INTEGER, INTEGER);
  53. DROP FUNCTION IF EXISTS aus_load_dbs_data(char(128),INTEGER);
  54. DROP FUNCTION IF EXISTS aus_load_dbs_data(char(128),INTEGER,INTEGER);
  55. DROP FUNCTION IF EXISTS aus_create_cmd_dist(char(128), INTEGER, CHAR(128), INTEGER,
  56. INTEGER, INTEGER, BIGINT);
  57. DROP FUNCTION IF EXISTS aus_get_exclusive_access(INTEGER, INTEGER);
  58. DROP FUNCTION IF EXISTS aus_rel_exclusive_access();
  59. DROP TABLE IF EXISTS aus_work_lock;
  60. DROP TABLE IF EXISTS aus_work_icols;
  61. DROP TABLE IF EXISTS aus_work_dist;
  62. DROP TABLE IF EXISTS aus_work_info;
  63. DROP TABLE IF EXISTS aus_work_icols;
  64. DROP TABLE IF EXISTS aus_work_dist;
  65. DROP TABLE IF EXISTS aus_work_info;
  66. DROP TABLE IF EXISTS aus_cmd_info;
  67. DROP TABLE IF EXISTS aus_cmd_list;
  68. DROP TABLE IF EXISTS aus_cmd_comp;
  69. {*********************************************************
  70. * Cleanup all configuration params
  71. ********************************************************}
  72. DELETE FROM ph_threshold WHERE task_name IN
  73. (
  74. "Auto Update Statistics Evaluation",
  75. "Auto Update Statistics Refresh"
  76. )
  77. OR
  78. name MATCHES "AUS_*";
  79. {*********************************************************
  80. * Remove the task from the database scheduler
  81. ********************************************************}
  82. UPDATE ph_task SET tk_attributes = BITANDNOT(tk_attributes, 4)
  83. WHERE tk_name MATCHES "Auto Update Statistics *";
  84. DELETE from ph_task WHERE tk_name MATCHES "Auto Update Statistics *";
  85. {* The following will reinstall AUS in sysadmin *}
  86. {*********************************************************
  87. *
  88. * The following section load all the parameters
  89. * utilized by AUS
  90. *
  91. ********************************************************
  92. *}
  93. INSERT INTO ph_threshold
  94. (id,name,task_name,value,value_type,description)
  95. VALUES
  96. (0,"AUS_AGE","Auto Update Statistics Evaluation","30","NUMERIC(6.2)",
  97. "The statistics are rebuilt after this many days.");
  98. INSERT INTO ph_threshold
  99. (id,name,task_name,value,value_type,description)
  100. VALUES
  101. (0,"AUS_CHANGE","Auto Update Statistics Evaluation","10","NUMERIC",
  102. "The statistics are rebuilt after this percentage of data has changed.");
  103. INSERT INTO ph_threshold
  104. (id,name,task_name,value,value_type,description)
  105. VALUES
  106. (0,"AUS_AUTO_RULES","Auto Update Statistics Evaluation","1","NUMERIC",
  107. "Ensures a base set of guidelines are followed when building statistics.");
  108. INSERT INTO ph_threshold
  109. (id,name,task_name,value,value_type,description)
  110. VALUES
  111. (0,"AUS_SMALL_TABLES","Auto Update Statistics Evaluation","100","NUMERIC",
  112. "Tables containing less than this number of rows will always have their statistics rebuilt.");
  113. INSERT INTO ph_threshold
  114. (id,name,task_name,value,value_type,description)
  115. VALUES
  116. (0,"AUS_PDQ","Auto Update Statistics Refresh","10","NUMERIC",
  117. "Update statistics executes with this PDQ priority.");
  118. {*********************************************************
  119. *
  120. * The following section creates all the procedures
  121. * utilized by AUS
  122. *
  123. ********************************************************
  124. *}
  125. CREATE FUNCTION informix.aus_get_realtime()
  126. RETURNING DATETIME YEAR TO SECOND
  127. DEFINE cur_time DATETIME YEAR TO SECOND;
  128. LET cur_time = (SELECT
  129. DBINFO( 'utc_to_datetime',sh_curtime)::DATETIME YEAR TO SECOND
  130. FROM sysmaster:sysshmvals);
  131. RETURN cur_time;
  132. END FUNCTION;
  133. CREATE FUNCTION informix.aus_setup_mon_table_profile(task_id INTEGER, task_seq INTEGER,
  134. inHDRmode INTEGER)
  135. RETURNING INTEGER
  136. DEFINE task_cnt INTEGER;
  137. DEFINE index_cnt INTEGER;
  138. --TRACE "FUNCTION aus_setup_mon_table_profile()" ;
  139. -- Make sure the task mon_table_profile is there an active
  140. SELECT count(*)
  141. INTO task_cnt
  142. FROM ph_task
  143. WHERE tk_name ="mon_table_profile";
  144. -- Check to see if index exists on (id,partnum)
  145. SELECT count(*)
  146. INTO index_cnt
  147. FROM sysindices
  148. WHERE tabid = ( SELECT tabid FROM systables
  149. WHERE tabname = "mon_table_profile" )
  150. AND ABS(ikeyextractcolno(indexkeys,0)) =
  151. ( SELECT colno FROM syscolumns
  152. WHERE tabid = ( SELECT tabid FROM systables
  153. WHERE tabname = "mon_table_profile"
  154. )
  155. AND colname ='id'
  156. )
  157. AND ABS(ikeyextractcolno(indexkeys,1)) =
  158. ( SELECT colno FROM syscolumns
  159. WHERE tabid = ( SELECT tabid FROM systables
  160. WHERE tabname = "mon_table_profile"
  161. )
  162. AND colname ='partnum'
  163. );
  164. IF task_cnt < 1 THEN
  165. INSERT INTO ph_alert
  166. (ID, alert_task_id,alert_task_seq,alert_type,
  167. alert_color, alert_object_type,
  168. alert_object_name, alert_message,alert_action)
  169. VALUES
  170. (0,task_id, task_seq, "WARNING", "green",
  171. "SERVER","Auto Update Statistics",
  172. "Built in server task (mon_table_profile) is missing. " ||
  173. "Auto Update Statistics proceeding with limited information.",
  174. NULL);
  175. RETURN -1;
  176. END IF;
  177. IF index_cnt < 1 THEN
  178. INSERT INTO ph_alert
  179. (ID, alert_task_id,alert_task_seq,alert_type,
  180. alert_color, alert_object_type,
  181. alert_object_name, alert_message,alert_action)
  182. VALUES
  183. (0,task_id, task_seq, "INFO", "green",
  184. "SERVER","Auto Update Statistics",
  185. "Building index on table mon_table_profile to optimize performance for Auto Update Statistics." ,
  186. NULL);
  187. IF inHDRmode = 1 THEN
  188. CREATE INDEX mon_table_profile_sys_ix1
  189. ON mon_table_profile(id,partnum) ONLINE;
  190. ELSE
  191. CREATE INDEX mon_table_profile_sys_ix1
  192. ON mon_table_profile(id,partnum);
  193. END IF;
  194. END IF;
  195. RETURN 1;
  196. END FUNCTION;
  197. CREATE FUNCTION informix.aus_get_exclusive_access(task_id INTEGER, task_seq INTEGER)
  198. RETURNING INTEGER
  199. DEFINE errnum INTEGER;
  200. DEFINE cnt INTEGER;
  201. DEFINE lk_sid INTEGER;
  202. --TRACE "FUNCTION aus_get_exclusive_access()" ;
  203. BEGIN
  204. ON EXCEPTION IN ( -310 ) SET errnum
  205. SELECT count(*) , MAX(aus_lk_sid)
  206. INTO cnt, lk_sid
  207. FROM aus_work_lock, sysmaster:sysscblst
  208. WHERE aus_lk_sid = sid
  209. AND aus_lk_task_id = task_id
  210. AND aus_lk_task_seq = task_seq;
  211. --TRACE "CNT = " || cnt || "lock SID = "||lk_sid;
  212. IF cnt > 0 THEN
  213. RAISE EXCEPTION -777, -107,
  214. "Current AUS tables are in use by Session ID "||lk_sid;
  215. END IF
  216. -- Fall through if count equal zero
  217. DELETE FROM aus_work_lock;
  218. END EXCEPTION WITH RESUME
  219. CREATE TABLE aus_work_lock (
  220. aus_lk_sid INTEGER,
  221. aus_lk_task_id INTEGER,
  222. aus_lk_task_seq INTEGER );
  223. INSERT INTO aus_work_lock(aus_lk_sid,aus_lk_task_id,aus_lk_task_seq)
  224. VALUES
  225. (DBINFO('sessionid'),task_id,task_seq);
  226. END
  227. RETURN 0;
  228. END FUNCTION;
  229. CREATE FUNCTION informix.aus_rel_exclusive_access()
  230. RETURNING INTEGER
  231. --TRACE "FUNCTION aus_rel_exclusive_access()" ;
  232. BEGIN
  233. ON EXCEPTION IN ( -206, -310 )
  234. END EXCEPTION
  235. DROP TABLE aus_work_lock;
  236. END
  237. RETURN 0;
  238. END FUNCTION;
  239. CREATE FUNCTION informix.aus_cleanup_table( save_results INTEGER )
  240. RETURNING INTEGER
  241. DEFINE errnum INTEGER;
  242. DEFINE cnt INTEGER;
  243. --TRACE "FUNCTION aus_cleanup_table()" ;
  244. IF save_results = 0 THEN
  245. SELECT COUNT(*)
  246. INTO cnt
  247. FROM ph_task
  248. WHERE BITAND(tk_attributes, '0x200' ) > 0
  249. AND tk_name like "Auto Update Statistics Refresh%"
  250. AND tk_next_execution <= ( SELECT tk_next_execution
  251. FROM ph_task
  252. WHERE tk_name = "Auto Update Statistics Evaluation");
  253. IF cnt > 0 THEN
  254. RAISE EXCEPTION -214, -107, "Current AUS tables are in use";
  255. END IF
  256. END IF
  257. /* Cleanup all the work tables */
  258. BEGIN
  259. ON EXCEPTION IN ( -214, -206, -394 ) SET errnum
  260. -- non-exclusive access, & table does not exist
  261. IF errnum = -214 THEN
  262. RAISE EXCEPTION -214, -107, "Current AUS tables are in use";
  263. END IF
  264. -- If error is -206 ignore the error, tables have been dropped
  265. END EXCEPTION WITH RESUME
  266. DROP TABLE aus_work_icols;
  267. DROP TABLE aus_work_dist;
  268. DROP TABLE aus_work_info;
  269. IF save_results == 0 THEN
  270. DROP TABLE aus_work_icols;
  271. DROP TABLE aus_work_dist;
  272. DROP TABLE aus_work_info;
  273. DROP TABLE aus_cmd_info;
  274. DROP VIEW aus_cmd_list;
  275. DROP VIEW aus_cmd_comp;
  276. DROP TABLE aus_command; -- drop views also
  277. END IF
  278. END
  279. RETURN 1;
  280. END FUNCTION;
  281. CREATE FUNCTION informix.aus_setup_table(inHDRmode INTEGER)
  282. RETURNING integer
  283. DEFINE errnum INTEGER;
  284. --TRACE "FUNCTION aus_setup_table()" ;
  285. /* Cleanup all the work tables */
  286. BEGIN
  287. ON EXCEPTION IN ( -310 ) SET errnum -- table already exists
  288. END EXCEPTION WITH RESUME
  289. CREATE TABLE informix.aus_cmd_info
  290. (
  291. aus_ci_dbs_partnum INTEGER,
  292. aus_ci_stime DATETIME YEAR TO SECOND
  293. DEFAULT CURRENT YEAR TO SECOND,
  294. aus_ci_etime DATETIME YEAR TO SECOND
  295. DEFAULT NULL,
  296. aus_ci_database VARCHAR(255) DEFAULT NULL,
  297. aus_ci_locale VARCHAR(36) DEFAULT NULL,
  298. aus_ci_logmode CHAR(1) DEFAULT NULL,
  299. aus_ci_missed_tables INTEGER DEFAULT 0,
  300. aus_ci_need_tables INTEGER DEFAULT 0,
  301. aus_ci_done_tables INTEGER DEFAULT 0
  302. );
  303. INSERT INTO aus_cmd_info(aus_ci_dbs_partnum,aus_ci_stime)
  304. VALUES (0,aus_get_realtime());
  305. IF inHDRmode = 1 THEN
  306. CREATE UNIQUE INDEX informix.aus_cmd_info_index1
  307. ON aus_cmd_info(aus_ci_dbs_partnum) ONLINE;
  308. CREATE UNIQUE INDEX informix.aus_cmd_info_index2
  309. ON aus_cmd_info(aus_ci_database) ONLINE;
  310. ELSE
  311. CREATE UNIQUE INDEX informix.aus_cmd_info_index1
  312. ON aus_cmd_info(aus_ci_dbs_partnum);
  313. CREATE UNIQUE INDEX informix.aus_cmd_info_index2
  314. ON aus_cmd_info(aus_ci_database);
  315. END IF;
  316. CREATE TABLE informix.aus_command
  317. (
  318. aus_cmd_id SERIAL,
  319. aus_cmd_state CHAR(1) DEFAULT 'P'
  320. CHECK (aus_cmd_state IN ("P","I","E","C")),
  321. -- P => Command is pending
  322. -- I => Command is inprogress
  323. -- E => Command had an Error
  324. -- C => Command is complete w/o Errors
  325. aus_cmd_type CHAR(1),
  326. aus_cmd_dbs_priority SMALLINT DEFAULT 2,
  327. aus_cmd_priority BIGINT,
  328. aus_cmd_dbs_partnum INTEGER,
  329. aus_cmd_partnum INTEGER,
  330. aus_cmd_err_sql INTEGER,
  331. aus_cmd_err_isam INTEGER,
  332. aus_cmd_time DATETIME YEAR TO SECOND DEFAULT CURRENT YEAR TO SECOND,
  333. aus_cmd_runtime INTERVAL HOUR TO SECOND DEFAULT NULL,
  334. aus_cmd_exe LVARCHAR(8192)
  335. ) LOCK MODE ROW;
  336. CREATE VIEW informix.aus_cmd_list AS SELECT
  337. aus_cmd_id, aus_cmd_type,
  338. aus_cmd_priority, aus_cmd_dbs_partnum,
  339. aus_cmd_partnum, aus_cmd_exe
  340. FROM aus_command
  341. WHERE aus_cmd_state = 'P';
  342. CREATE VIEW informix.aus_cmd_comp AS SELECT
  343. aus_cmd_id, aus_cmd_type,
  344. aus_cmd_priority, aus_cmd_dbs_partnum,
  345. aus_cmd_partnum, aus_cmd_exe,
  346. aus_cmd_time
  347. FROM aus_command
  348. WHERE aus_cmd_state = 'C';
  349. END
  350. BEGIN
  351. ON EXCEPTION IN ( -310 ) SET errnum -- table already exists
  352. END EXCEPTION WITH RESUME
  353. /*
  354. * This table contains both
  355. * index columns as wells as
  356. * user defined distribution columns
  357. */
  358. CREATE TABLE informix.aus_work_icols
  359. (
  360. aus_icols_tabid integer,
  361. aus_icols_colno integer,
  362. aus_icols_lkey char(1) CHECK( aus_icols_lkey IN ('Y','N')),
  363. aus_icols_mode char(1) DEFAULT NULL,
  364. aus_icols_colname varchar(128)
  365. ) LOCK MODE ROW;
  366. CREATE TABLE informix.aus_work_info
  367. (
  368. aus_info_id SERIAL,
  369. aus_info_db_partnum INTEGER,
  370. aus_info_tabname VARCHAR(128),
  371. aus_info_tabid INTEGER,
  372. aus_info_partnum INTEGER,
  373. aus_info_ustlowts DATETIME YEAR TO SECOND,
  374. aus_info_npused BIGINT,
  375. aus_info_nrows BIGINT,
  376. aus_info_nindexes smallint
  377. ) LOCK MODE ROW;
  378. CREATE TABLE informix.aus_work_dist
  379. (
  380. aus_dist_id serial,
  381. aus_dist_tabid INTEGER, -- this is the tabid
  382. aus_dist_colno INTEGER,
  383. aus_dist_mode CHAR(1),
  384. aus_dist_resolution FLOAT,
  385. aus_dist_confidence FLOAT,
  386. aus_dist_smplsize float,
  387. aus_dist_rowssmplde BIGINT,
  388. aus_dist_constr_time DATETIME YEAR TO SECOND,
  389. aus_dist_ustnrows BIGINT
  390. ) LOCK MODE ROW;
  391. END
  392. RETURN 1;
  393. END FUNCTION;
  394. CREATE FUNCTION informix.aus_refresh_stats_orig( )
  395. RETURNING integer
  396. DEFINE rc INTEGER;
  397. DEFINE cnt INTEGER;
  398. DEFINE i INTEGER;
  399. LET rc=1;
  400. LET i=1;
  401. WHILE ( rc <> 0 ) LOOP
  402. LET rc = aus_refresh_stats_orig(-1,i);
  403. LET i=i+1;
  404. END LOOP;
  405. RETURN rc;
  406. END FUNCTION;
  407. CREATE FUNCTION informix.aus_refresh_stats_orig(task_id INTEGER, task_seq INTEGER)
  408. RETURNING integer
  409. DEFINE rc INTEGER;
  410. DEFINE cnt INTEGER;
  411. DEFINE del INTEGER;
  412. DEFINE t_partnum INTEGER;
  413. DEFINE last_partnum INTEGER;
  414. DEFINE t_dbs_partnum INTEGER;
  415. DEFINE last_dbs_partnum INTEGER;
  416. DEFINE t_id INTEGER;
  417. DEFINE t_type CHAR(1);
  418. DEFINE t_cmd CHAR(8192);
  419. DEFINE p_cmd CHAR(200);
  420. DEFINE t_priority BIGINT;
  421. DEFINE param_pdq INTEGER;
  422. --TRACE "FUNCTION aus_refresh_stats_orig";
  423. LET rc = 0;
  424. LET cnt = 0;
  425. LET del = 0;
  426. LET last_partnum = 0;
  427. LET t_partnum = NULL;
  428. --SET DEBUG FILE TO "/tmp/aus.refresh."||task_seq;
  429. --TRACE ON;
  430. -- Get the config thresholds
  431. SELECT MAX(value::integer) INTO param_pdq
  432. FROM sysadmin:ph_threshold WHERE name = "AUS_PDQ";
  433. IF param_pdq IS NULL THEN
  434. LET param_pdq = 0;
  435. ELIF param_pdq < 0 THEN
  436. LET param_pdq = 0;
  437. ELIF param_pdq > 100 THEN
  438. LET param_pdq = 100;
  439. END IF
  440. IF param_pdq > 0 AND is_pdq_allowed() > 0 THEN
  441. LET p_cmd = "SET PDQPRIORITY " ||param_pdq;
  442. END IF
  443. BEGIN
  444. ON EXCEPTION IN ( -206 ) -- Exit if aus_cmd_* tables are not found
  445. END EXCEPTION
  446. FOREACH SELECT --+ FIRST_ROWS
  447. aus_cmd_id, RTRIM(aus_cmd_exe),aus_cmd_partnum, aus_cmd_priority,
  448. aus_cmd_type, aus_cmd_dbs_partnum
  449. INTO
  450. t_id, t_cmd, t_partnum, t_priority, t_type , t_dbs_partnum
  451. FROM aus_command
  452. WHERE aus_cmd_state = 'P' -- Pending
  453. ORDER BY aus_cmd_priority DESC, aus_cmd_partnum, aus_cmd_type DESC
  454. --TRACE "LAST partnum ="||last_partnum;
  455. IF last_partnum == 0 THEN
  456. LET last_partnum = t_partnum;
  457. LET last_dbs_partnum = t_dbs_partnum;
  458. LET cnt = cnt + 1;
  459. ELIF last_partnum <> t_partnum THEN
  460. LET last_partnum = t_partnum;
  461. IF t_priority > 100000 THEN
  462. IF last_dbs_partnum <> t_dbs_partnum THEN
  463. CONTINUE FOREACH;
  464. END IF
  465. IF cnt > 100 THEN
  466. EXIT FOREACH;
  467. END IF
  468. LET cnt = cnt + 1;
  469. ELSE
  470. EXIT FOREACH;
  471. END IF
  472. END IF
  473. --TRACE "UPDATING cmd_id="||t_id ||" TABLE "|| t_partnum||" MODE " || RTRIM(t_type)::lvarchar ||" CNT =" ||cnt;
  474. DELETE FROM aus_command WHERE aus_cmd_id = t_id;
  475. LET del = DBINFO("sqlca.sqlerrd2");
  476. /* If we did not delete anything then skip this row, parallel */
  477. IF del < 0 THEN
  478. CONTINUE FOREACH;
  479. END IF
  480. BEGIN
  481. ON EXCEPTION IN ( -206 ) -- IGNORE if the table is gone
  482. END EXCEPTION WITH RESUME
  483. EXECUTE IMMEDIATE p_cmd;
  484. EXECUTE IMMEDIATE t_cmd;
  485. IF is_pdq_allowed() > 0 THEN
  486. SET PDQPRIORITY 0;
  487. END IF
  488. END
  489. INSERT INTO aus_command
  490. ( aus_cmd_id, aus_cmd_type, aus_cmd_priority, aus_cmd_state,
  491. aus_cmd_dbs_partnum, aus_cmd_partnum, aus_cmd_exe )
  492. VALUES
  493. ( t_id, t_type, t_priority, 'C', t_dbs_partnum, t_partnum, TRIM(t_cmd));
  494. LET rc=rc+1;
  495. END FOREACH
  496. IF cnt > 0 THEN
  497. UPDATE aus_cmd_info
  498. SET (aus_ci_done_tables) = (aus_ci_done_tables + cnt)
  499. WHERE aus_ci_dbs_partnum = last_dbs_partnum;
  500. ELIF task_id > 0 THEN
  501. /* I have no more work to and I am a real task disable my self */
  502. UPDATE ph_task SET ( tk_enable ) = ('F') WHERE tk_id = task_id;
  503. END IF
  504. END
  505. RETURN rc;
  506. END FUNCTION;
  507. CREATE FUNCTION informix.aus_enable_refresh()
  508. RETURNING INTEGER
  509. UPDATE ph_task SET ( tk_enable) =
  510. ('T')
  511. WHERE tk_name matches "Auto Update Statistics Refresh*";
  512. END FUNCTION;
  513. CREATE FUNCTION informix.aus_create_cmd_dist(dbsname char(128), dbs_partnum INTEGER,
  514. tabname CHAR(128), partnum INTEGER, tabid INTEGER,
  515. param_rules INTEGER, expire_priority BIGINT)
  516. RETURNING BIGINT
  517. DEFINE rc INTEGER;
  518. DEFINE cnt INTEGER;
  519. DEFINE collist CHAR(7600);
  520. DEFINE maxcollength INTEGER;
  521. DEFINE collistlen INTEGER;
  522. DEFINE collen INTEGER;
  523. DEFINE t_colno INTEGER;
  524. DEFINE t_resolution DECIMAL(5,3);
  525. DEFINE last_resolution DECIMAL(5,3);
  526. DEFINE t_confidence DECIMAL(5,3);
  527. DEFINE t_smplsize DECIMAL(16,3);
  528. DEFINE t_colname CHAR(128);
  529. DEFINE tmp CHAR(128);
  530. DEFINE lasttmp CHAR(128);
  531. DEFINE end_str CHAR(256);
  532. --TRACE "FUNCTION aus_create_cmd_dist( database=" || RTRIM(dbsname) || " tabid = " || tabid || " )" ;
  533. LET collist = NULL;
  534. LET rc = 0;
  535. /* Note the l must be lower case, so we
  536. * can order by later can get the order as
  537. * H, M, l
  538. * when building commands.
  539. */
  540. INSERT INTO aus_command
  541. ( aus_cmd_id, aus_cmd_type, aus_cmd_priority, aus_cmd_dbs_partnum, aus_cmd_partnum, aus_cmd_exe )
  542. VALUES
  543. ( 0, "l", expire_priority, dbs_partnum, partnum,
  544. "UPDATE STATISTICS LOW FOR TABLE " || RTRIM(dbsname) || ":"
  545. || RTRIM(tabname)
  546. );
  547. /* FIND ALL HIGH COLUMNS
  548. * The first part finds all column
  549. * which were previously in sysdistrib
  550. * The second part SQL is executed if
  551. * the auto rules are enable, it finds
  552. * the minimum set of columns for this
  553. * table for the optimizer
  554. */
  555. UPDATE aus_work_icols SET ( aus_icols_mode ) = ( 'H' )
  556. WHERE aus_icols_tabid = tabid
  557. AND aus_icols_colno in ( SELECT aus_dist_colno FROM aus_work_dist
  558. WHERE aus_dist_tabid = tabid
  559. AND aus_dist_mode = 'H');
  560. --TRACE "EXISTING HIGH ROWS UPDATED " || DBINFO("sqlca.sqlerrd2") || ")";
  561. IF param_rules > 0 THEN
  562. UPDATE aus_work_icols SET ( aus_icols_mode ) = ( decode(aus_icols_lkey,'Y','H','M') )
  563. WHERE aus_icols_tabid = tabid
  564. AND aus_icols_mode IS NULL;
  565. --TRACE "PARAM_RULES ROWS UPDATED " || DBINFO("sqlca.sqlerrd2") || ")";
  566. ELSE
  567. SELECT count(*) INTO cnt
  568. FROM aus_work_icols
  569. WHERE aus_icols_mode IS NULL AND aus_icols_tabid = tabid;
  570. IF cnt > 0 THEN
  571. UPDATE aus_cmd_info SET
  572. (aus_ci_missed_tables) = (aus_ci_missed_tables + 1)
  573. WHERE aus_ci_database = dbsname;
  574. END IF
  575. END IF
  576. /** HIGH COLUMNS **/
  577. LET collist = NULL;
  578. LET maxcollength = 7600;
  579. LET collistlen = 0;
  580. LET collen = 0;
  581. LET tmp = NULL;
  582. LET lasttmp = NULL;
  583. FOREACH SELECT
  584. aus_icols_colno, aus_icols_colname, NVL(aus_dist_resolution,0.5)
  585. INTO
  586. t_colno, t_colname, t_resolution
  587. FROM aus_work_icols , OUTER aus_work_dist
  588. WHERE aus_icols_tabid = aus_dist_tabid
  589. AND aus_icols_colno = aus_dist_colno
  590. AND aus_icols_tabid = tabid
  591. AND aus_icols_mode = 'H'
  592. ORDER BY aus_dist_resolution, aus_dist_colno
  593. IF collist IS NULL THEN
  594. LET collist = RTRIM(t_colname);
  595. LET collistlen = LENGTH(TRIM(collist));
  596. LET last_resolution = t_resolution;
  597. LET end_str = " RESOLUTION " || t_resolution || " DISTRIBUTIONS ONLY";
  598. ELIF last_resolution == t_resolution THEN
  599. LET collen = LENGTH(RTRIM(t_colname));
  600. -- TRACE "collist len: " || collistlen;
  601. IF collen + collistlen + 2 >= maxcollength THEN
  602. INSERT INTO aus_command
  603. ( aus_cmd_id, aus_cmd_type, aus_cmd_priority,
  604. aus_cmd_dbs_partnum, aus_cmd_partnum, aus_cmd_exe )
  605. VALUES
  606. ( 0, "H", expire_priority, dbs_partnum, partnum,
  607. "UPDATE STATISTICS HIGH FOR TABLE " || RTRIM(dbsname)
  608. || ":" || RTRIM(tabname)
  609. || " ( " || TRIM(collist) || " ) " || TRIM(end_str)
  610. );
  611. LET collist = RTRIM(t_colname);
  612. LET collistlen = LENGTH(TRIM(collist));
  613. ELSE
  614. LET collist = RTRIM(collist) || ", " ||
  615. RTRIM(t_colname);
  616. LET collistlen = collistlen + collen + 2;
  617. END IF
  618. ELSE
  619. INSERT INTO aus_command
  620. ( aus_cmd_id, aus_cmd_type, aus_cmd_priority,
  621. aus_cmd_dbs_partnum, aus_cmd_partnum, aus_cmd_exe )
  622. VALUES
  623. ( 0, "H", expire_priority, dbs_partnum, partnum,
  624. "UPDATE STATISTICS HIGH FOR TABLE " || RTRIM(dbsname)
  625. || ":" || RTRIM(tabname)
  626. || " ( " || TRIM(collist) || " ) " || TRIM(end_str)
  627. );
  628. LET collist = TRIM(t_colname);
  629. LET collistlen = LENGTH(TRIM(collist));
  630. LET end_str = " RESOLUTION " || t_resolution || " DISTRIBUTIONS ONLY";
  631. LET last_resolution = t_resolution;
  632. END IF
  633. --TRACE "COLLIST LENGTH: " || LENGTH(TRIM(collist));
  634. --TRACE "HIGH Colname " || RTRIM(t_colname);
  635. END FOREACH
  636. IF collist IS NOT NULL THEN
  637. INSERT INTO aus_command
  638. ( aus_cmd_id, aus_cmd_type, aus_cmd_priority,
  639. aus_cmd_dbs_partnum, aus_cmd_partnum, aus_cmd_exe )
  640. VALUES
  641. ( 0, "H", expire_priority, dbs_partnum, partnum,
  642. "UPDATE STATISTICS HIGH FOR TABLE "|| RTRIM(dbsname)
  643. || ":" || RTRIM(tabname)
  644. || " ( "|| RTRIM(collist) || " ) " || TRIM(end_str)
  645. );
  646. END IF
  647. /*
  648. -- FIND ALL MED COLUMNS
  649. */
  650. UPDATE aus_work_icols SET ( aus_icols_mode ) = ( 'M' )
  651. WHERE aus_icols_tabid = tabid
  652. AND aus_icols_colno in ( SELECT aus_dist_colno FROM aus_work_dist
  653. WHERE aus_dist_tabid = tabid
  654. AND aus_dist_mode = 'M');
  655. LET collist = NULL;
  656. LET collistlen = 0;
  657. LET collen = 0;
  658. LET tmp = NULL;
  659. LET lasttmp = NULL;
  660. FOREACH SELECT
  661. aus_icols_colno, aus_icols_colname,
  662. NVL(aus_dist_resolution,2.0), NVL(aus_dist_confidence,0.95),
  663. NVL(aus_dist_smplsize,0.0),
  664. NVL(aus_dist_smplsize,0)::DECIMAL(16,3) || "_" ||
  665. NVL(aus_dist_resolution,0)::DECIMAL(5,2) || "_" ||
  666. NVL(aus_dist_confidence,0)::DECIMAL(4,2) AS C
  667. INTO
  668. t_colno, t_colname,
  669. t_resolution, t_confidence,
  670. t_smplsize, tmp
  671. FROM aus_work_icols , OUTER aus_work_dist
  672. WHERE aus_icols_tabid = aus_dist_tabid
  673. AND aus_icols_colno = aus_dist_colno
  674. AND aus_icols_tabid = tabid
  675. AND aus_icols_mode = 'M'
  676. ORDER BY C, aus_dist_colno
  677. --TRACE "MED Colname " || RTRIM(t_colname) || " tmp" || RTRIM(tmp);
  678. --TRACE "collist " || TRIM(NVL(collist,"NULL"));
  679. --TRACE "lasttmp " || TRIM(NVL(lasttmp,"NULL"));
  680. --TRACE "tmp " || TRIM(NVL(tmp,"NULL"));
  681. IF collist IS NULL THEN
  682. LET collist = TRIM(t_colname);
  683. LET collistlen = LENGTH(TRIM(collist));
  684. LET lasttmp = tmp;
  685. LET end_str = decode(t_smplsize,0.0," "," SAMPLING SIZE "
  686. || t_smplsize )
  687. || " RESOLUTION "|| t_resolution
  688. || " " || t_confidence
  689. || " DISTRIBUTIONS ONLY";
  690. --TRACE "NULL collist" || TRIM(t_colname) || " tmp" || TRIM(tmp);
  691. ELIF lasttmp IS NULL OR lasttmp == tmp THEN
  692. --TRACE "equal collist" || TRIM(t_colname) || " tmp" || TRIM(tmp);
  693. LET collen = LENGTH(RTRIM(t_colname));
  694. --TRACE "collist len: " || collistlen;
  695. IF collen + collistlen + 2 >= maxcollength THEN
  696. INSERT INTO aus_command
  697. ( aus_cmd_id, aus_cmd_type, aus_cmd_priority,
  698. aus_cmd_dbs_partnum, aus_cmd_partnum, aus_cmd_exe )
  699. VALUES
  700. ( 0, "M", expire_priority, dbs_partnum, partnum,
  701. "UPDATE STATISTICS MEDIUM FOR TABLE "
  702. || RTRIM(dbsname) || ":" || TRIM(tabname)
  703. || " (" || TRIM(collist) || ") " || TRIM(end_str)
  704. );
  705. LET collist = RTRIM(t_colname);
  706. LET collistlen = LENGTH(TRIM(collist));
  707. ELSE
  708. LET collist = RTRIM(collist) || ", "
  709. || RTRIM(t_colname);
  710. LET collistlen = collistlen + collen + 2;
  711. END IF
  712. ELSE
  713. INSERT INTO aus_command
  714. ( aus_cmd_id, aus_cmd_type, aus_cmd_priority,
  715. aus_cmd_dbs_partnum, aus_cmd_partnum, aus_cmd_exe )
  716. VALUES
  717. ( 0, "M", expire_priority, dbs_partnum, partnum,
  718. "UPDATE STATISTICS MEDIUM FOR TABLE " || RTRIM(dbsname)
  719. || ":" || TRIM(tabname)
  720. || " ("|| TRIM(collist) || ") " || TRIM(end_str)
  721. );
  722. LET collist = TRIM(t_colname);
  723. LET collistlen = LENGTH(TRIM(collist));
  724. LET lasttmp = tmp;
  725. LET end_str = decode(t_smplsize,0.0," "," SAMPLING SIZE "
  726. || t_smplsize )
  727. || " RESOLUTION " || t_resolution
  728. || " " || t_confidence
  729. || " DISTRIBUTIONS ONLY";
  730. END IF
  731. --TRACE "MED Colname " || TRIM(t_colname);
  732. END FOREACH
  733. IF collist IS NOT NULL THEN
  734. INSERT INTO aus_command
  735. ( aus_cmd_id, aus_cmd_type, aus_cmd_priority,
  736. aus_cmd_dbs_partnum, aus_cmd_partnum, aus_cmd_exe )
  737. VALUES
  738. ( 0, "M", expire_priority, dbs_partnum, partnum,
  739. "UPDATE STATISTICS MEDIUM FOR TABLE " ||
  740. TRIM(dbsname) || ":" || TRIM(tabname)
  741. || " (" || TRIM(collist) || ") " || TRIM(end_str)
  742. );
  743. END IF
  744. RETURN 0;
  745. END FUNCTION;
  746. CREATE FUNCTION informix.aus_evaluator_dbs(dbsname CHAR(128), dbs_partnum INTEGER,
  747. has_mon_tab_prof INTEGER, inHDRmode INTEGER)
  748. RETURNING integer
  749. DEFINE aus_if_id INTEGER;
  750. DEFINE aus_if_tabname CHAR(128);
  751. DEFINE aus_if_tabid INTEGER;
  752. DEFINE aus_if_partnum INTEGER;
  753. DEFINE aus_if_ustlowts DATETIME YEAR TO SECOND;
  754. DEFINE aus_if_npused BIGINT;
  755. DEFINE aus_if_nrows BIGINT;
  756. DEFINE aus_real_rows BIGINT;
  757. DEFINE aus_if_nindexes INTEGER;
  758. DEFINE oldest_time DATETIME YEAR TO SECOND;
  759. DEFINE expired_value BIGINT;
  760. DEFINE max_seq_id INTEGER;
  761. DEFINE min_seq_id INTEGER;
  762. DEFINE hours_expired BIGINT;
  763. DEFINE nrows, isreads, iswrite, isrwrite, isdelete FLOAT;
  764. DEFINE delta_time BIGINT;
  765. DEFINE small_tab_exp BIGINT;
  766. DEFINE tmin BIGINT;
  767. DEFINE tmp BIGINT;
  768. DEFINE param_age_stats INTEGER; -- Number of hours
  769. DEFINE param_change INTEGER;
  770. DEFINE param_rules INTEGER;
  771. DEFINE param_small_tab BIGINT;
  772. DEFINE row_count INTEGER;
  773. DEFINE cmd CHAR(1000);
  774. --TRACE "FUNCTION aus_evaluator_dbs(database="||TRIM(dbsname)::lvarchar||")" ;
  775. -- Get the config thresholds
  776. SELECT MAX(value::float * 24)::integer INTO param_age_stats
  777. FROM sysadmin:ph_threshold WHERE name = "AUS_AGE";
  778. SELECT MAX(value::integer) INTO param_change
  779. FROM sysadmin:ph_threshold WHERE name = "AUS_CHANGE";
  780. SELECT MAX(value::integer) INTO param_rules
  781. FROM sysadmin:ph_threshold WHERE name = "AUS_AUTO_RULES";
  782. SELECT MAX(value::bigint) INTO param_small_tab
  783. FROM sysadmin:ph_threshold WHERE name = "AUS_SMALL_TABLES";
  784. /* Default values if not found in the config table */
  785. LET param_age_stats = NVL(param_age_stats ,30*24);
  786. LET param_change = NVL(param_change ,10);
  787. LET param_rules = NVL(param_rules ,1);
  788. LET param_small_tab = NVL(param_small_tab ,100);
  789. IF param_small_tab > 200000 THEN
  790. /* Can not let be higher than 200,000 as
  791. * will cause overflow of variable
  792. */
  793. LET param_small_tab = 200000;
  794. END IF
  795. LET row_count = 0;
  796. /* Loop through the specified database */
  797. FOREACH SELECT
  798. aus_info_id, aus_info_tabname, aus_info_tabid,
  799. aus_info_partnum, aus_info_ustlowts, aus_info_npused,
  800. aus_info_nrows, aus_info_nindexes
  801. INTO
  802. aus_if_id, aus_if_tabname, aus_if_tabid,
  803. aus_if_partnum, aus_if_ustlowts, aus_if_npused,
  804. aus_if_nrows, aus_if_nindexes
  805. FROM aus_work_info
  806. WHERE aus_info_db_partnum = dbs_partnum
  807. ORDER BY aus_info_ustlowts
  808. IF aus_if_partnum = 0 THEN
  809. LET cmd = "UPDATE aus_work_info SET (aus_info_nrows) = ("
  810. || " (SELECT sum(pt.nrows)"
  811. || " FROM sysmaster:sysptnhdr pt"
  812. || " WHERE pt.partnum IN (SELECT partn FROM "
  813. || TRIM(dbsname)::lvarchar||":sysfragments sfg"
  814. || " WHERE sfg.tabid = " || aus_if_tabid
  815. || " AND sfg.fragtype = 'T')))"
  816. || " WHERE aus_info_tabid = " || aus_if_tabid ;
  817. EXECUTE IMMEDIATE cmd;
  818. LET cmd = "UPDATE aus_work_info"
  819. || " SET(aus_info_partnum) = ((SELECT min(lockid)"
  820. || " FROM sysmaster:sysptnhdr ptn, "
  821. || TRIM(dbsname)::lvarchar||":sysfragments"
  822. || " sfg WHERE ptn.partnum = sfg.partn"
  823. || " AND sfg.tabid = " || aus_if_tabid
  824. || " AND sfg.fragtype = 'T' ))"
  825. || " WHERE aus_info_tabid = " || aus_if_tabid ;
  826. EXECUTE IMMEDIATE cmd;
  827. SELECT aus_info_nrows
  828. INTO aus_real_rows
  829. FROM aus_work_info
  830. WHERE aus_info_tabid = aus_if_tabid;
  831. ELSE
  832. SELECT sum(pt.nrows)
  833. INTO aus_real_rows
  834. FROM sysmaster:sysptnhdr pt
  835. WHERE pt.partnum = aus_if_partnum;
  836. END IF
  837. --TRACE "Checking table "||TRIM(dbsname)::lvarchar||"." || TRIM(aus_if_tabname)::lvarchar;
  838. --TRACE "Number of rows "||aus_real_rows;
  839. LET expired_value = 0;
  840. /* If the table is considered a small row table
  841. * expire the table and create the commands.
  842. */
  843. LET tmp = param_small_tab - aus_real_rows;
  844. IF tmp > 0 THEN
  845. LET small_tab_exp = tmp + 10000 * param_small_tab;
  846. ELSE
  847. LET small_tab_exp = 0;
  848. END IF
  849. /* Calculate the oldesttime between
  850. * the statistics and distributions
  851. */
  852. SELECT MIN(aus_dist_constr_time)
  853. INTO oldest_time
  854. FROM aus_work_dist
  855. WHERE aus_dist_tabid = aus_if_tabid
  856. AND aus_dist_constr_time IS NOT NULL ;
  857. IF oldest_time IS NULL AND aus_if_ustlowts IS NULL THEN
  858. --TRACE "No Statistics and distributions for "|| TRIM(dbsname)::lvarchar||"." || TRIM(aus_if_tabname)::lvarchar;
  859. IF param_rules == 0 THEN
  860. /* If we have no stats or dist and we are not
  861. * doing auto rules then continue to the next table
  862. */
  863. UPDATE aus_cmd_info SET
  864. (aus_ci_missed_tables) = (aus_ci_missed_tables + 1)
  865. WHERE aus_ci_dbs_partnum = dbs_partnum;
  866. CONTINUE FOREACH;
  867. ELIF small_tab_exp == 0 THEN
  868. /* We have auto rules on, but this tables is not small.
  869. */
  870. LET expired_value = 100;
  871. ELSE
  872. /* We have auto rules on, and a small table
  873. */
  874. LET expired_value = small_tab_exp;
  875. END IF
  876. GOTO do_commands;
  877. END IF
  878. IF oldest_time IS NULL THEN
  879. LET oldest_time = aus_if_ustlowts;
  880. ELIF (aus_if_ustlowts IS NOT NULL) AND (aus_if_ustlowts < oldest_time) THEN
  881. LET oldest_time = aus_if_ustlowts;
  882. END IF
  883. --TRACE "Oldest time for table "|| TRIM(aus_if_tabname)::lvarchar || " is "|| oldest_time;
  884. /* If we have a small table and
  885. * we have any stats or dist OR we are doing auto rules
  886. * THEN goto make the commands
  887. */
  888. IF (small_tab_exp > 0) AND
  889. ( oldest_time IS NOT NULL OR param_rules <> 0 ) THEN
  890. LET expired_value = small_tab_exp;
  891. GOTO do_commands;
  892. END IF
  893. /* If we are not applying basic rules
  894. * and we have no upd stats low information
  895. * for this table before then skip
  896. * this table now and look for distributions.
  897. */
  898. IF param_rules == 0 AND (aus_if_nrows IS NULL OR aus_if_ustlowts IS NULL) THEN
  899. GOTO do_distribution_eval;
  900. END IF
  901. /*
  902. * Check the age of the statistics
  903. * If never been done then assign 100
  904. */
  905. --TRACE "Start statistics checking basic row count " || expired_value;
  906. --TRACE "aus_if_nrows "|| NVL(aus_if_nrows,"NULL");
  907. --TRACE "aus_real_rows "|| NVL(aus_real_rows,"NULL");
  908. IF aus_if_nrows IS NULL OR aus_real_rows IS NULL THEN
  909. LET expired_value = expired_value + 100;
  910. ELSE
  911. LET tmp = (aus_real_rows - aus_if_nrows) / (aus_if_nrows+1) * 100;
  912. --TRACE "Change in basic row count " || tmp ||"%";
  913. IF ABS(tmp) > param_change THEN
  914. LET expired_value = expired_value + ABS(tmp);
  915. END IF
  916. END IF
  917. /*
  918. * Check the basic row count of the statistics
  919. * If never been done then assign 100
  920. */
  921. --TRACE "Start statistics checking for age " || expired_value ;
  922. IF oldest_time IS NULL THEN
  923. LET expired_value = expired_value + 100;
  924. ELSE
  925. --TRACE "param_age_stats "||param_age_stats;
  926. LET hours_expired = (
  927. ( ( (CURRENT - oldest_time)::INTERVAL HOUR(5) TO HOUR
  928. - param_age_stats UNITS HOUR))::char(20))::BIGINT;
  929. --TRACE "hours_expired "||hours_expired;
  930. IF hours_expired > 0 THEN
  931. LET expired_value = expired_value + hours_expired/24;
  932. END IF
  933. END IF
  934. --TRACE "INFO statistics checking basic row count END " || expired_value;
  935. <<do_distribution_eval>>
  936. --TRACE "Statitics checking advanced row count START " || expired_value;
  937. IF oldest_time IS NOT NULL AND has_mon_tab_prof == 1 THEN
  938. -- Find the MAX/MIN sequence numbers to use for this table statistics
  939. -- Currently we do not take into account onstat -z
  940. SELECT MAX(run.run_task_seq),
  941. MIN(run.run_task_seq),
  942. ((((MAX(run.run_time) - MIN(run.run_time))::
  943. INTERVAL MINUTE(9) TO MINUTE)::char(20))::BIGINT)
  944. INTO max_seq_id, min_seq_id, delta_time
  945. FROM ph_run run, ph_task task
  946. WHERE task.tk_id = run.run_task_id
  947. AND tk_name = "mon_table_profile"
  948. AND run.run_time > oldest_time;
  949. --TRACE "Delta Time "|| delta_time ||" max task_seq "||max_seq_id||" min task_seq "||min_seq_id;
  950. /* If we had 0 or 1 measurement then delta_time will be 0
  951. * we must skip this check, not enough data
  952. */
  953. IF delta_time > 0 THEN
  954. LET tmin = (((CURRENT - oldest_time)::
  955. INTERVAL MINUTE(9) TO MINUTE)::char(20))::BIGINT;
  956. { Get the number of UDIS for statistics }
  957. --TRACE "SELECT " || aus_if_partnum ;
  958. SELECT
  959. tmin * ((SUM(aft.nrows) - SUM(bef.nrows))/delta_time),
  960. tmin * ((SUM(aft.pf_isread) - SUM(bef.pf_isread))/delta_time),
  961. tmin * ((SUM(aft.pf_iswrite) - SUM(bef.pf_iswrite))/delta_time),
  962. tmin * ((SUM(aft.pf_isrwrite) - SUM(bef.pf_isrwrite))/delta_time),
  963. tmin * ((SUM(aft.pf_isdelete) - SUM(bef.pf_isdelete))/delta_time)
  964. INTO nrows, isreads, iswrite, isrwrite, isdelete
  965. FROM mon_table_profile bef, mon_table_profile aft
  966. WHERE aft.id = max_seq_id
  967. AND bef.id = min_seq_id
  968. AND aft.lockid = aus_if_partnum
  969. AND bef.lockid = aus_if_partnum
  970. AND bef.lockid = aft.lockid;
  971. --TRACE "Inserts " || iswrite || "Update " || isrwrite|| "Delete " || isdelete;
  972. LET tmp = (iswrite + isrwrite + isdelete) - ( aus_real_rows * param_change/100);
  973. --TRACE "Rows Changed " || (iswrite + isrwrite + isdelete) || " Precent Changed " || (iswrite + isrwrite + isdelete) / aus_real_rows ;
  974. IF tmp > 0 THEN
  975. LET expired_value = expired_value + 100 + tmp;
  976. END IF
  977. END IF
  978. END IF
  979. /*
  980. * DONE calculating the statistics ranking
  981. */
  982. IF expired_value == 0 THEN
  983. CONTINUE FOREACH;
  984. END IF
  985. <<do_commands>>
  986. --TRACE "Building stats on table "||TRIM(dbsname)::lvarchar||"." || TRIM(aus_if_tabname)::lvarchar;
  987. /* Build the update low stats command if required */
  988. LET tmp = aus_create_cmd_dist(dbsname, dbs_partnum, aus_if_tabname,
  989. aus_if_partnum, aus_if_tabid,
  990. param_rules , expired_value );
  991. --TRACE "NEW CMD row_count="||row_count;
  992. IF tmp < 0 THEN
  993. RETURN tmp;
  994. END IF
  995. LET row_count = row_count + 1;
  996. END FOREACH --- End aus_dbs_list FOREACH
  997. RETURN row_count;
  998. END FUNCTION;
  999. CREATE FUNCTION informix.aus_load_dbs_data(aus_dbsname CHAR(128), aus_dbs_partnum INTEGER
  1000. ,inHDRmode INTEGER)
  1001. RETURNING bigint
  1002. DEFINE tmp CHAR(1500);
  1003. DEFINE nrows BIGINT;
  1004. DEFINE drows BIGINT;
  1005. LET nrows = 0;
  1006. LET drows = 0;
  1007. /*
  1008. ***********************************************************
  1009. * Load the aus_work_info table from systables
  1010. ************************************************************
  1011. */
  1012. LET tmp = "INSERT INTO aus_work_info ("
  1013. || " aus_info_id, aus_info_tabname, aus_info_tabid, aus_info_partnum, "
  1014. || " aus_info_ustlowts, aus_info_npused, aus_info_nrows, "
  1015. || " aus_info_nindexes, aus_info_db_partnum "
  1016. || " ) SELECT "
  1017. || " 0, tabname, tabid, partnum, "
  1018. || " ustlowts, npused, nrows, "
  1019. || " nindexes, "
  1020. || aus_dbs_partnum
  1021. || " FROM "
  1022. || TRIM(aus_dbsname)::lvarchar||":systables t "
  1023. || " WHERE "
  1024. || " t.tabtype='T'"
  1025. ;
  1026. EXECUTE IMMEDIATE tmp ;
  1027. --TRACE "Loading "||TRIM(aus_dbsname)::lvarchar||":aus_work_info with ROWS(" || DBINFO("sqlca.sqlerrd2")||")";
  1028. LET nrows = nrows + DBINFO("sqlca.sqlerrd2");
  1029. IF (aus_dbsname == "sysadmin") THEN
  1030. DELETE FROM aus_work_info WHERE aus_info_tabname LIKE "aus_work_%"
  1031. AND aus_info_db_partnum = aus_dbs_partnum;
  1032. LET nrows = nrows - 4;
  1033. END IF;
  1034. IF inHDRmode = 1 THEN
  1035. CREATE INDEX informix.aus_work_info_idx1 ON aus_work_info(aus_info_tabid)
  1036. ONLINE;
  1037. CREATE INDEX informix.aus_work_info_idx2 ON aus_work_info(aus_info_partnum)
  1038. ONLINE;
  1039. ELSE
  1040. CREATE INDEX informix.aus_work_info_idx1 ON aus_work_info(aus_info_tabid);
  1041. CREATE INDEX informix.aus_work_info_idx2 ON aus_work_info(aus_info_partnum);
  1042. END IF
  1043. /*
  1044. ***********************************************************
  1045. * Load the aus_work_dist table from sysdistrib table
  1046. ************************************************************
  1047. */
  1048. LET tmp = "INSERT INTO aus_work_dist ("
  1049. || " aus_dist_id, aus_dist_tabid, aus_dist_colno, "
  1050. || " aus_dist_mode, aus_dist_resolution, aus_dist_confidence, "
  1051. || " aus_dist_smplsize, aus_dist_rowssmplde, aus_dist_constr_time, "
  1052. || " aus_dist_ustnrows "
  1053. || " ) SELECT "
  1054. || " 0, tabid, colno, "
  1055. || " mode, resolution, confidence, "
  1056. || " smplsize,rowssmpld,constr_time, "
  1057. || " ustnrows"
  1058. || " FROM " || TRIM(aus_dbsname)::lvarchar||":sysdistrib d "
  1059. || " WHERE d.seqno=1 "
  1060. ;
  1061. EXECUTE IMMEDIATE tmp ;
  1062. LET drows = drows + DBINFO("sqlca.sqlerrd2");
  1063. IF inHDRmode = 1 THEN
  1064. CREATE INDEX informix.aus_work_dist_idx1 ON aus_work_dist
  1065. (aus_dist_tabid,aus_dist_colno) ONLINE;
  1066. ELSE
  1067. CREATE INDEX informix.aus_work_dist_idx1 ON aus_work_dist
  1068. (aus_dist_tabid,aus_dist_colno);
  1069. END IF
  1070. --TRACE "Loading "||TRIM(aus_dbsname)::lvarchar||":aus_work_dist with ROWS(" || DBINFO("sqlca.sqlerrd2")||")";
  1071. /* We need to update the fragment tables partnum with
  1072. * the lock id, so we can reference them as one item.
  1073. * Also get the current number of rows.
  1074. UPDATE aus_work_info
  1075. SET ( aus_info_partnum, aus_info_nrows) =
  1076. ( ( SELECT MAX(lockid), SUM(nrows)
  1077. FROM sysmaster:systabnames t, sysmaster:sysptnhdr pt
  1078. WHERE t.partnum = pt.partnum
  1079. AND t.partnum = aus_info_partnum
  1080. AND t.tabname = aus_info_tabname
  1081. AND t.dbsname = aus_dbsname) )
  1082. WHERE aus_info_db_partnum = aus_dbs_partnum
  1083. AND aus_info_nrows = -1
  1084. ;
  1085. */
  1086. /*
  1087. ***********************************************************
  1088. * Load the aus_work_icols table of all index colnames
  1089. ************************************************************
  1090. */
  1091. LET tmp = "CREATE VIEW informix.aus_view (tabid,colno, colname,lkey) AS "
  1092. || " SELECT "
  1093. || " C.tabid, C.colno, MAX(C.colname), 'N' "
  1094. || " FROM "
  1095. || TRIM(aus_dbsname)::lvarchar||":syscolumns AS C, "
  1096. || TRIM(aus_dbsname)::lvarchar||":sysindices AS I "
  1097. || " WHERE "
  1098. || " C.tabid = I.tabid "
  1099. || " AND C.colno in "
  1100. || " (ABS(ikeyextractcolno(indexkeys,0)), "
  1101. || " ABS(ikeyextractcolno(indexkeys,1)) , "
  1102. || " ABS(ikeyextractcolno(indexkeys,2)) , "
  1103. || " ABS(ikeyextractcolno(indexkeys,3)) , "
  1104. || " ABS(ikeyextractcolno(indexkeys,4)) , "
  1105. || " ABS(ikeyextractcolno(indexkeys,5)) , "
  1106. || " ABS(ikeyextractcolno(indexkeys,6)) , "
  1107. || " ABS(ikeyextractcolno(indexkeys,7)) , "
  1108. || " ABS(ikeyextractcolno(indexkeys,8)) , "
  1109. || " ABS(ikeyextractcolno(indexkeys,9)) , "
  1110. || " ABS(ikeyextractcolno(indexkeys,10)) , "
  1111. || " ABS(ikeyextractcolno(indexkeys,11)) , "
  1112. || " ABS(ikeyextractcolno(indexkeys,12)) , "
  1113. || " ABS(ikeyextractcolno(indexkeys,13)) , "
  1114. || " ABS(ikeyextractcolno(indexkeys,14)) , "
  1115. || " ABS(ikeyextractcolno(indexkeys,15)) ) "
  1116. || " GROUP BY 1,2,4 "
  1117. || "UNION "
  1118. || "SELECT "
  1119. || " C.tabid, C.colno, MAX(C.colname), 'N' "
  1120. || " FROM "
  1121. || TRIM(aus_dbsname)::lvarchar||":syscolumns AS C, "
  1122. || "aus_work_dist AS D "
  1123. || " WHERE C.tabid = D.aus_dist_tabid "
  1124. || " AND C.colno = D.aus_dist_colno"
  1125. || " GROUP BY 1,2,4 "
  1126. ;
  1127. EXECUTE IMMEDIATE tmp ;
  1128. LET tmp = "INSERT INTO aus_work_icols"
  1129. || "(aus_icols_tabid, aus_icols_colno,aus_icols_colname,aus_icols_lkey) "
  1130. || " SELECT tabid, colno, colname,lkey FROM aus_view";
  1131. EXECUTE IMMEDIATE tmp ;
  1132. --TRACE "Loading "||TRIM(aus_dbsname)::lvarchar||":aus_work_icols with ROWS(" || DBINFO("sqlca.sqlerrd2")||")";
  1133. DROP VIEW aus_view;
  1134. IF inHDRmode = 1 THEN
  1135. CREATE INDEX informix.aus_work_icols_idx1 ON aus_work_icols
  1136. (aus_icols_tabid,aus_icols_colno) ONLINE;
  1137. ELSE
  1138. CREATE INDEX informix.aus_work_icols_idx1 ON aus_work_icols
  1139. (aus_icols_tabid,aus_icols_colno);
  1140. END IF
  1141. LET tmp = "UPDATE aus_work_icols SET aus_icols_lkey = 'Y' "
  1142. || "WHERE aus_icols_tabid*65536 + aus_icols_colno "
  1143. || " IN ( "
  1144. || " SELECT tabid*65536 + ABS(ikeyextractcolno(indexkeys,0)) "
  1145. || "FROM " || TRIM(aus_dbsname)::lvarchar||":sysindices)";
  1146. EXECUTE IMMEDIATE tmp ;
  1147. --TRACE "Updating lead keys for aus_work_icols," || DBINFO("sqlca.sqlerrd2")||" lead keys found";
  1148. RETURN nrows;
  1149. END FUNCTION;
  1150. CREATE FUNCTION informix.aus_evaluator(eval_only BOOLEAN)
  1151. RETURNING BIGINT
  1152. IF eval_only THEN
  1153. RETURN aus_evaluator(-1, 1, 0);
  1154. END IF
  1155. RETURN aus_evaluator(-1, 1, 1);
  1156. END FUNCTION;
  1157. CREATE FUNCTION informix.aus_evaluator(task_id INTEGER, task_seq INTEGER)
  1158. RETURNING BIGINT
  1159. RETURN aus_evaluator( task_id, task_seq, 1);
  1160. END FUNCTION;
  1161. CREATE FUNCTION informix.aus_evaluator(task_id INTEGER, task_seq INTEGER,myflags INTEGER)
  1162. RETURNING BIGINT
  1163. DEFINE aus_dbsname CHAR(128);
  1164. DEFINE aus_dbs_partnum INTEGER;
  1165. DEFINE trows BIGINT;
  1166. DEFINE nrows BIGINT;
  1167. DEFINE has_mon_tab_prof INTEGER;
  1168. DEFINE param_debug CHAR(128);
  1169. DEFINE dblocale CHAR(36);
  1170. DEFINE logmode CHAR(3);
  1171. DEFINE errnum INTEGER;
  1172. DEFINE inHDRmode INTEGER;
  1173. LET nrows = 0;
  1174. LET trows = 0;
  1175. SELECT FIRST 1 value INTO param_debug
  1176. FROM sysadmin:ph_threshold WHERE name = "AUS_DEBUG";
  1177. IF param_debug IS NOT NULL THEN
  1178. --SET DEBUG FILE TO TRIM(param_debug)::lvarchar||".prc";
  1179. --TRACE PROCEDURE;
  1180. --TRACE ON;
  1181. ELSE
  1182. --SET DEBUG FILE TO "/tmp/aus.debug.off";
  1183. --TRACE OFF;
  1184. --TRACE ON;
  1185. END IF
  1186. LET errnum = aus_get_exclusive_access(task_id, task_seq);
  1187. LET errnum = aus_cleanup_table(0);
  1188. SELECT FIRST 1 DECODE(type, "Primary", 1, "Standard", 0, 0) INTO inHDRmode
  1189. FROM sysmaster:sysdri;
  1190. IF (inHDRmode = 1) THEN
  1191. SET LOCK MODE TO WAIT 120;
  1192. ELSE
  1193. SET LOCK MODE TO WAIT 5;
  1194. END IF
  1195. LET errnum = aus_setup_table(inHDRmode);
  1196. SET ISOLATION TO DIRTY READ;
  1197. LET has_mon_tab_prof = aus_setup_mon_table_profile(task_id,task_seq,inHDRmode);
  1198. IF has_mon_tab_prof == 1 THEN
  1199. LET nrows = exectask( "mon_table_profile" );
  1200. END IF
  1201. /* Loop through all the logging databases
  1202. * loading up the aus_work_info table
  1203. */
  1204. --TRACE "Begin examining each logging database";
  1205. FOREACH SELECT TRIM(name), partnum
  1206. INTO aus_dbsname, aus_dbs_partnum
  1207. FROM sysmaster:sysdbspartn
  1208. WHERE partnum NOT IN
  1209. (select partnum
  1210. FROM sysmaster:sysdbspartn
  1211. WHERE bitand(flags, 3 ) > 0
  1212. AND name NOT IN (SELECT value FROM sysadmin:ph_threshold
  1213. WHERE name = "AUS_DATABASE_DISABLED" ))
  1214. INSERT INTO ph_alert
  1215. (ID, alert_task_id,alert_task_seq,alert_type,
  1216. alert_color, alert_object_type,
  1217. alert_object_name, alert_message,alert_action)
  1218. VALUES
  1219. (0,task_id, task_seq, "INFO", "yellow", "DATABASE",
  1220. "Auto Update Statistics",
  1221. "Skipping database ["||trim(aus_dbsname)||
  1222. "] for Auto Update Statistics. ",
  1223. NULL);
  1224. END FOREACH
  1225. FOREACH SELECT TRIM(D.name), D.partnum,
  1226. decode(bitand(D.flags,5),0,'N',4,'A','L') as logmode, T.collate
  1227. INTO aus_dbsname, aus_dbs_partnum, logmode, dblocale
  1228. FROM sysmaster:sysdbspartn D, sysmaster:systabnames T
  1229. WHERE bitand(D.flags, 3 ) > 0 --- Not ansi yet need owner names
  1230. AND D.name <> "sysmaster"
  1231. AND D.partnum = T.partnum
  1232. AND D.name NOT IN (SELECT value FROM sysadmin:ph_threshold
  1233. WHERE name = "AUS_DATABASE_DISABLED" )
  1234. INSERT INTO aus_cmd_info
  1235. (aus_ci_dbs_partnum, aus_ci_need_tables,
  1236. aus_ci_database, aus_ci_locale, aus_ci_logmode, aus_ci_stime)
  1237. VALUES
  1238. (aus_dbs_partnum, 0,
  1239. aus_dbsname, dblocale, logmode, aus_get_realtime());
  1240. --TRACE "Starting database loading " || RTRIM(aus_dbsname);
  1241. LET nrows = aus_load_dbs_data(aus_dbsname, aus_dbs_partnum, inHDRmode);
  1242. --TRACE "Starting database evaluation";
  1243. LET trows = aus_evaluator_dbs(aus_dbsname, aus_dbs_partnum,
  1244. has_mon_tab_prof, inHDRmode);
  1245. UPDATE aus_cmd_info
  1246. SET (aus_ci_need_tables,aus_ci_etime) =
  1247. (aus_ci_need_tables + trows, aus_get_realtime())
  1248. WHERE aus_ci_database = aus_dbsname;
  1249. IF trows < 0 THEN
  1250. EXIT FOREACH;
  1251. END IF
  1252. IF trows > 0 THEN
  1253. INSERT INTO ph_alert
  1254. (ID, alert_task_id,alert_task_seq,alert_type,
  1255. alert_color, alert_object_type,
  1256. alert_object_name, alert_message,alert_action)
  1257. VALUES
  1258. (0,task_id, task_seq, "INFO", "yellow", "DATABASE",
  1259. "Auto Update Statistics",
  1260. "Found "||trows||" table(s) in database ["||trim(aus_dbsname)|| "]"
  1261. || " which need statistics updated. ",
  1262. NULL);
  1263. ELSE
  1264. INSERT INTO ph_alert
  1265. (ID, alert_task_id,alert_task_seq,alert_type,
  1266. alert_color, alert_object_type,
  1267. alert_object_name, alert_message,alert_action)
  1268. VALUES
  1269. (0,task_id, task_seq, "INFO", "green", "DATABASE",
  1270. "Auto Update Statistics",
  1271. "Found "||trows||" table(s) in database ["||trim(aus_dbsname)|| "]"
  1272. || " which need statistics updated.",
  1273. NULL);
  1274. END IF
  1275. DROP INDEX aus_work_info_idx1;
  1276. DROP INDEX aus_work_info_idx2;
  1277. DROP INDEX aus_work_dist_idx1;
  1278. DROP INDEX aus_work_icols_idx1;
  1279. TRUNCATE TABLE aus_work_info;
  1280. TRUNCATE TABLE aus_work_dist;
  1281. TRUNCATE TABLE aus_work_icols;
  1282. END FOREACH --- End Database FOREACH
  1283. UPDATE aus_cmd_info
  1284. SET (aus_ci_etime) = ( aus_get_realtime())
  1285. WHERE aus_ci_database IS NULL;
  1286. LET errnum = aus_cleanup_table(1);
  1287. IF inHDRmode = 1 THEN
  1288. CREATE INDEX informix.aus_command_ix1 ON aus_command(aus_cmd_id) ONLINE;
  1289. CREATE INDEX informix.aus_command_ix2 ON aus_command( aus_cmd_state,
  1290. aus_cmd_priority DESC, aus_cmd_partnum, aus_cmd_type DESC)
  1291. ONLINE;
  1292. ELSE
  1293. CREATE INDEX informix.aus_command_ix1 ON aus_command(aus_cmd_id);
  1294. CREATE INDEX informix.aus_command_ix2 ON aus_command(aus_cmd_state,
  1295. aus_cmd_priority DESC, aus_cmd_partnum, aus_cmd_type DESC);
  1296. END IF
  1297. LET errnum = aus_rel_exclusive_access();
  1298. IF bitand(myflags,1) > 0 THEN
  1299. LET errnum = aus_enable_refresh();
  1300. END IF
  1301. RETURN nrows;
  1302. END FUNCTION;
  1303. {*********************************************************
  1304. *
  1305. * The following section create the task in the database
  1306. * scheduler used by the AUS feature
  1307. *
  1308. ********************************************************
  1309. *}
  1310. DELETE from ph_task WHERE tk_name = "Auto Update Statistics Evaluation";
  1311. INSERT INTO ph_task
  1312. (
  1313. tk_name,
  1314. tk_type,
  1315. tk_group,
  1316. tk_description,
  1317. tk_execute,
  1318. tk_start_time,
  1319. tk_stop_time,
  1320. tk_frequency,
  1321. tk_enable
  1322. )
  1323. VALUES
  1324. (
  1325. "Auto Update Statistics Evaluation",
  1326. "TASK",
  1327. "PERFORMANCE",
  1328. "To Evaluate which columns and tables should have the statistics and distributions refreshed.",
  1329. "aus_evaluate_stats",
  1330. DATETIME(01:00:00) HOUR TO SECOND,
  1331. DATETIME(01:10:00) HOUR TO SECOND,
  1332. INTERVAL ( 1 ) DAY TO DAY,
  1333. 't'
  1334. );
  1335. DELETE from ph_task WHERE tk_name = "Auto Update Statistics Refresh";
  1336. INSERT INTO ph_task
  1337. (
  1338. tk_name,
  1339. tk_type,
  1340. tk_group,
  1341. tk_description,
  1342. tk_execute,
  1343. tk_start_time,
  1344. tk_stop_time,
  1345. tk_frequency,
  1346. tk_monday,
  1347. tk_Tuesday,
  1348. tk_Wednesday,
  1349. tk_Thursday,
  1350. tk_Friday,
  1351. tk_sunday,
  1352. tk_saturday,
  1353. tk_enable
  1354. )
  1355. VALUES
  1356. (
  1357. "Auto Update Statistics Refresh",
  1358. "TASK",
  1359. "PERFORMANCE",
  1360. "Refreshes the statistics and distributions which were recommended by the evaluator.",
  1361. "aus_refresh_stats",
  1362. DATETIME(01:11:00) HOUR TO SECOND,
  1363. DATETIME(05:00:00) HOUR TO SECOND,
  1364. INTERVAL ( 1 ) DAY TO DAY,
  1365. 'f',
  1366. 'f',
  1367. 'f',
  1368. 'f',
  1369. 'f',
  1370. 't',
  1371. 't',
  1372. 't'
  1373. );
  1374. create function aus_evaluate_stats(task_id integer, seq_id integer)
  1375. returns integer
  1376. external name '(aus_evaluator)'
  1377. language C;
  1378. CREATE FUNCTION aus_evaluator_upgrade()
  1379. RETURNING INTEGER
  1380. UPDATE ph_task SET
  1381. (tk_execute) = ( "aus_evaluate_stats")
  1382. WHERE tk_name = 'Auto Update Statistics Evaluation';
  1383. RETURN 0;
  1384. END FUNCTION;
  1385. CREATE FUNCTION aus_evaluator_downgrade()
  1386. RETURNING INTEGER
  1387. UPDATE ph_task SET
  1388. (tk_execute) = ( "aus_evaluator")
  1389. WHERE tk_name = 'Auto Update Statistics Evaluation';
  1390. RETURN 0;
  1391. END FUNCTION;
  1392. create function informix.aus_refresh_stats(task_id integer, seq_id integer,
  1393. run_time integer default 0)
  1394. returns integer
  1395. external name '(aus_refresh)'
  1396. language C;
  1397. CREATE FUNCTION informix.aus_refresh_upgrade()
  1398. RETURNING INTEGER
  1399. UPDATE ph_task SET
  1400. ( tk_execute, tk_enable ) = ( "aus_refresh_stats", 't' )
  1401. WHERE tk_name = 'Auto Update Statistics Refresh';
  1402. UPDATE ph_task SET
  1403. ( tk_frequency ) = ( INTERVAL ( 1 ) DAY TO DAY )
  1404. WHERE tk_name = 'Auto Update Statistics Refresh'
  1405. AND tk_frequency < INTERVAL ( 1 ) DAY TO DAY;
  1406. RETURN 0;
  1407. END FUNCTION;
  1408. CREATE FUNCTION informix.aus_refresh_downgrade()
  1409. RETURNING INTEGER
  1410. UPDATE ph_task SET
  1411. ( tk_execute, tk_enable, tk_frequency ) =
  1412. ( "aus_refresh_stats_orig", 'f', INTERVAL ( 1 ) SECOND TO SECOND )
  1413. WHERE tk_name = 'Auto Update Statistics Refresh';
  1414. RETURN 0;
  1415. END FUNCTION;
  1416. {**************************************************************************
  1417. MARK THE ABOVE TASKS AS SYSTEM TASKS AND FOR PRIVATE THREAD
  1418. **************************************************************************}
  1419. UPDATE ph_task SET tk_attributes = BITOR(tk_attributes, 12)
  1420. WHERE tk_name
  1421. IN ('Auto Update Statistics Evaluation',
  1422. 'Auto Update Statistics Refresh');
  1423. {**************************************************************************
  1424. RECORD THE VERSION OF AUS INSTALLED
  1425. **************************************************************************}
  1426. DELETE FROM ph_version WHERE object = 'AUS' and type = 'version';
  1427. INSERT INTO ph_version(object,type,value) VALUES ( 'AUS','version', 23 );