Database | Proc | Application | Created | Links |
sybsystemprocs | sp_cmp_qplans ![]() | ![]() | 31 Aug 14 | Defects Dependencies |
1 2 /* Sccsid = "%Z% generic/sproc/%M% %I% %G%" */ 3 4 /* 5 ** Messages for "sp_cmp_qplans" 6 ** 7 ** 18636, "There is no query plan with the ID %1! in this database." 8 ** 18692, "The queries are the same." 9 ** 18693, "The queries are different." 10 ** 18694, "The queries are different but have the same hash key." 11 ** 18695, "The query plans are the same." 12 ** 18696, "The query plans are different." 13 ** 18640, "Unknown %1! option : '%2!'. Valid options are : %3!." 14 */ 15 16 /* 17 ** sp_cmp_qplans compares 2 plans, and returns : 18 ** 19 ** 0 - same query/plan 20 ** +1 - different queries 21 ** +2 - different queries and hash collision (does not accumulate with +1) 22 ** +10 - different plans 23 ** 100 - plan ID does not exist 24 ** 25 ** For instance, 2 different APs that belong to 2 different queries 26 ** that collide on the hash key will return 2+10 = 12. 27 ** 28 ** Accepts an @mode parameter, when "quiet" only returns status, 29 ** when "brief" also gives messages, no "verbose" mode. 30 */ 31 32 create procedure sp_cmp_qplans 33 @id1 int, 34 @id2 int, 35 @mode varchar(20) = "brief" 36 as 37 declare 38 @uid int, 39 @h1 int, 40 @h2 int, 41 @query int, 42 @plan int, 43 @msg varchar(1024) 44 45 set nocount on 46 47 /* Don't start a transaction for the user, he does not expect it. */ 48 if @@trancount = 0 49 begin 50 set chained off 51 end 52 53 /* Don't hold long READ locks, the user might not appreciate it. */ 54 set transaction isolation level 1 55 56 57 select @uid = nullif (user_id(), 1) 58 59 /* check the hash keys first */ 60 61 select @h1 = (select distinct hashkey 62 from sysqueryplans 63 where 64 uid = isnull(@uid, uid) 65 and id = @id1) 66 67 select @h2 = (select distinct hashkey 68 from sysqueryplans 69 where 70 uid = isnull(@uid, uid) 71 and id = @id2) 72 73 if @h1 is null 74 /* 18636, "There is no query plan with the ID %1! in this database." */ 75 raiserror 18636, @id1 76 77 if @h2 is null 78 /* 18636, "There is no query plan with the ID %1! in this database." */ 79 raiserror 18636, @id2 80 81 /* fail if either ID is absent */ 82 if @h1 is null or @h2 is null 83 return 100 84 85 if @h1 != @h2 86 begin 87 select @query = 1 88 goto plans 89 end 90 91 /* hash collision at least */ 92 select @query = 2 93 94 if 95 (select count(*) 96 from sysqueryplans 97 where 98 id = @id1 99 and type = 10) 100 != 101 (select count(*) 102 from sysqueryplans 103 where 104 id = @id2 105 and type = 10) 106 goto plans 107 108 if exists (select * 109 from sysqueryplans p1, sysqueryplans p2 110 where 111 p1.id = @id1 112 and p2.id = @id2 113 and p1.type = 10 114 and p2.type = 10 115 and p1.sequence = p2.sequence 116 and isnull(rtrim(p1.text), " ") != isnull(rtrim(p2.text), " ")) 117 goto plans 118 119 /* same query */ 120 select @query = 0 121 122 plans: 123 124 /* assume different plans */ 125 select @plan = 10 126 127 if 128 (select count(*) 129 from sysqueryplans 130 where 131 id = @id1 132 and type = 100) 133 != 134 (select count(*) 135 from sysqueryplans 136 where 137 id = @id2 138 and type = 100) 139 goto ret 140 141 if exists (select * 142 from sysqueryplans p1, sysqueryplans p2 143 where 144 p1.id = @id1 145 and p2.id = @id2 146 and p1.type = 100 147 and p2.type = 100 148 and p1.sequence = p2.sequence 149 and isnull(rtrim(p1.text), " ") != isnull(rtrim(p2.text), " ")) 150 goto ret 151 152 /* same plan */ 153 select @plan = 0 154 155 ret: 156 157 if @mode = "brief" 158 begin 159 /* query related messages */ 160 if @query = 0 161 begin 162 /* 18692, "The queries are the same." */ 163 exec sp_getmessage 18692, @msg out 164 end 165 else if @query = 1 166 begin 167 /* 18693, "The queries are different." */ 168 exec sp_getmessage 18693, @msg out 169 end 170 else if @query = 2 171 begin 172 /* 18694, "The queries are different but have the same hash key." */ 173 exec sp_getmessage 18694, @msg out 174 end 175 /* else wrong @query - do nothing */ 176 177 print @msg 178 179 /* plan related messages */ 180 if @plan = 0 181 begin 182 /* 18695, "The query plans are the same." */ 183 exec sp_getmessage 18695, @msg out 184 end 185 else if @plan = 10 186 begin 187 /* 18696, "The query plans are different." */ 188 exec sp_getmessage 18696, @msg out 189 end 190 /* else wrong @plan - do nothing */ 191 192 print @msg 193 194 end 195 else if @mode != "quiet" 196 begin 197 /* 18640, "Unknown %1! option : '%2!'. Valid options are : %3!." */ 198 raiserror 18640, "sp_cmp_qplans", @mode, "'quiet', 'brief'" 199 end 200 201 /* 202 ** 0 - same query/plan 203 ** +1 - different queries 204 ** +2 - different queries and hash collision 205 ** +10 - different plans 206 ** 100 - plan ID does not exist 207 */ 208 return @query + @plan 209 210
exec sp_procxmode 'sp_cmp_qplans', 'AnyMode' go Grant Execute on sp_cmp_qplans to public go
DEFECTS | |
![]() (uid, gid, hashkey, id, type, sequence) Intersection: {id} | 65 |
![]() (uid, gid, hashkey, id, type, sequence) Intersection: {id} | 71 |
![]() (uid, gid, hashkey, id, type, sequence) Intersection: {type, id} | 98 |
![]() (uid, gid, hashkey, id, type, sequence) Intersection: {type, id} | 104 |
![]() (uid, gid, hashkey, id, type, sequence) Intersection: {sequence} Uncovered: [uid, gid, hashkey] | 115 |
![]() (uid, gid, hashkey, id, type, sequence) Intersection: {type, id} | 131 |
![]() (uid, gid, hashkey, id, type, sequence) Intersection: {type, id} | 137 |
![]() (uid, gid, hashkey, id, type, sequence) Intersection: {sequence} Uncovered: [uid, gid, hashkey] | 148 |
![]() | 99 |
![]() | 105 |
![]() | 113 |
![]() | 114 |
![]() | 132 |
![]() | 138 |
![]() | 146 |
![]() | 147 |
![]() | |
![]() | |
![]() | 163 |
![]() | 168 |
![]() | 173 |
![]() | 183 |
![]() | 188 |
![]() | 61 |
![]() | 67 |
![]() | 54 |
![]() | 109 |
![]() | 142 |
![]() | 61 |
![]() | 67 |
![]() | 95 |
![]() | 101 |
![]() | 108 |
![]() | 128 |
![]() | 134 |
![]() | 141 |
![]() | 32 |
![]() | 32 |
![]() | 32 |
![]() | 108 |
![]() | 141 |
DEPENDENCIES |
PROCS AND TABLES USED reads table sybsystemprocs..sysqueryplans ![]() calls proc sybsystemprocs..sp_getmessage ![]() reads table sybsystemprocs..sysusermessages ![]() reads table master..sysmessages (1) ![]() reads table master..syslanguages (1) ![]() calls proc sybsystemprocs..sp_validlang ![]() reads table master..syslanguages (1) ![]() CALLERS called by proc sybsystemprocs..sp_copy_qplan ![]() called by proc sybsystemprocs..sp_copy_all_qplans ![]() |