query_police 0.1.0.beta
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +13 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +12 -0
- data/LICENSE.txt +21 -0
- data/README.md +487 -0
- data/Rakefile +12 -0
- data/lib/query_police/analysis/dynamic_message.rb +72 -0
- data/lib/query_police/analysis.rb +155 -0
- data/lib/query_police/config.rb +17 -0
- data/lib/query_police/constants.rb +14 -0
- data/lib/query_police/explain.rb +70 -0
- data/lib/query_police/helper.rb +44 -0
- data/lib/query_police/rules.json +171 -0
- data/lib/query_police/version.rb +5 -0
- data/lib/query_police.rb +163 -0
- data/sig/query_police.rbs +4 -0
- metadata +104 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 89db06f142d97fcf351d3c2bdadb121a66a4684e9570293ebe15655cb2cbd62d
|
4
|
+
data.tar.gz: d5d235f590005a175512b9b807afb606634ab28635c4b466220f537e981567d2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9ebdfdcdf9fd986143cab1e481a38260294b7588530a2ce79b6e5066651fc0c38dff8d7c3caf445789da9c5d115e75eab5bb4a136ece64647006687b8ecb0e70
|
7
|
+
data.tar.gz: f98f685595b74e8e0c45a0308f9f933ecd647de4268a6505184fed24649497d9aee3e0636269347f78ca15bf4d6b32fb56b0149b2653243c048f4be49163c252
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at striker.aryu56@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 strikeraryu
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,487 @@
|
|
1
|
+
# QueryPolice
|
2
|
+
|
3
|
+
It is a rule-based engine with custom rules to Analyze Active-Record relations using explain results to detect bad queries.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Install the gem and add to the application's Gemfile by executing:
|
8
|
+
|
9
|
+
$ bundle add query_police
|
10
|
+
|
11
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
12
|
+
|
13
|
+
$ gem install query_police
|
14
|
+
|
15
|
+
---
|
16
|
+
|
17
|
+
## Get started
|
18
|
+
|
19
|
+
### Basic Usage
|
20
|
+
|
21
|
+
Use `QueryPolice.analyse` to generate an analysis object for your Active-Record relation or query and then you can use pretty print on the object.
|
22
|
+
|
23
|
+
```
|
24
|
+
analysis = QueryPolice.analyse(<active_record_relation>)
|
25
|
+
puts analysis.pretty_analysis_for(<impact>)
|
26
|
+
```
|
27
|
+
|
28
|
+
**Eg.**
|
29
|
+
```
|
30
|
+
analysis = QueryPolice.analyse(
|
31
|
+
User.joins('join sessions on sessions.user_email = users.email ')
|
32
|
+
.where('sessions.created_at < ?', Time.now - 5.months).order('sessions.created_at')
|
33
|
+
)
|
34
|
+
puts analysis.pretty_analysis_for('negative')
|
35
|
+
# or
|
36
|
+
puts analysis.pretty_analysis({'negative' => true, 'positive' => true})
|
37
|
+
```
|
38
|
+
**Results**
|
39
|
+
|
40
|
+
```
|
41
|
+
table: sessions
|
42
|
+
column: type
|
43
|
+
impact: negative
|
44
|
+
message: Entire sessions table is scanned to find matching rows, you have 1 possible keys to use.
|
45
|
+
suggestion: Use index here. You can use index from possible key: ["index_sessions_on_user_email"] or add new one to sessions table as per the requirements.
|
46
|
+
column: key
|
47
|
+
impact: negative
|
48
|
+
message: There is no index key used for sessions table, and can result into full scan of the sessions table
|
49
|
+
suggestion: Please use index from possible_keys: ["index_sessions_on_user_email"] or add new one to sessions table as per the requirements.
|
50
|
+
column: rows
|
51
|
+
impact: negative
|
52
|
+
message: 2982924 rows are being scanned per join for sessions table.
|
53
|
+
suggestion: Please see if it is possible to use index from ["index_sessions_on_user_email"] or add new one to sessions table as per the requirements to reduce the number of rows scanned.
|
54
|
+
```
|
55
|
+
|
56
|
+
### Add logger for every query
|
57
|
+
|
58
|
+
Add `QueryPolice.subscribe_logger` to your initial load file like `application.rb`
|
59
|
+
|
60
|
+
You can make logger silence of error using `QueryPolice.subscribe_logger silent: true`.
|
61
|
+
|
62
|
+
You can change logger config using `QueryPolice logger_config: <config>`, default logger_config `{'negative' => true}`, options `positive: <Boolean>, caution: <Boolean>`.
|
63
|
+
|
64
|
+
|
65
|
+
|
66
|
+
## How it works?
|
67
|
+
|
68
|
+
1. Query police converts the relation into sql query
|
69
|
+
|
70
|
+
2. Query police generates execution plan using EXPLAIN and EXPLAIN format=JSON based on the configuration.
|
71
|
+
|
72
|
+
3. Query police load rules from the config file.
|
73
|
+
|
74
|
+
4. Query police apply rules on the execution plan and generate a new analysis object.
|
75
|
+
|
76
|
+
5. Analysis object provide different methods to print the analysis in more descriptive format.
|
77
|
+
|
78
|
+
|
79
|
+
## Execution plan
|
80
|
+
|
81
|
+
We have 2 possible execution plan:-
|
82
|
+
|
83
|
+
Normal - using `EXPLAIN`
|
84
|
+
|
85
|
+
Detailed - using `EXPLAIN format=JSON`
|
86
|
+
|
87
|
+
**NOTE:** By default Detailed execution plan is added in the final execution plan, you can remove that by `QueryPolice.detailed=false`
|
88
|
+
|
89
|
+
### Normal execution plan
|
90
|
+
|
91
|
+
Generated using `EXPAIN <query>`
|
92
|
+
|
93
|
+
**Result**
|
94
|
+
|
95
|
+
| id | select_type | table | partitions | type. | possible_keys | key | key_len | ref | rows | filtered | Extra |
|
96
|
+
|----|-------------|---------|------------|--------|---------------------------|---------------------|---------|-----------------------------|------|----------|--------------------------|
|
97
|
+
| 1 | SIMPLE | profile | NULL. | index | fk_rails_249a7ebca1 | fk_rails_249a7ebca1 | 5 | NULL | 603 | 100.00 | Using where; Using index |
|
98
|
+
| 1 | SIMPLE | users | NULL | eq_ref | PRIMARY,index_users_on_id | PRIMARY | 4 | development.profile.user_id |1 | 100.00 | NULL |
|
99
|
+
|
100
|
+
|
101
|
+
Result for this is added as it is in the final execution plan
|
102
|
+
|
103
|
+
**Eg.**
|
104
|
+
|
105
|
+
```
|
106
|
+
{
|
107
|
+
"profile" => {
|
108
|
+
"id" => 1,
|
109
|
+
"select_type" => "SIMPLE",
|
110
|
+
"table" => "profile",
|
111
|
+
"partitions" => nil,
|
112
|
+
"type" => "index",
|
113
|
+
"possible_keys" => "fk_rails_249a7ebca1",
|
114
|
+
"key" => "fk_rails_249a7ebca1",
|
115
|
+
"key_len" => "5",
|
116
|
+
"ref" => nil,
|
117
|
+
"rows" => 603,
|
118
|
+
"filtered" => 100.0,
|
119
|
+
"Extra" => "Using where; Using index"
|
120
|
+
},
|
121
|
+
"users" => {
|
122
|
+
"id" => 1,
|
123
|
+
"select_type" => "SIMPLE",
|
124
|
+
"table" => "users",
|
125
|
+
"partitions" => nil,
|
126
|
+
"type" => "eq_ref",
|
127
|
+
"possible_keys" => "PRIMARY,index_users_on_id",
|
128
|
+
"key" => "PRIMARY",
|
129
|
+
"key_len" => "4",
|
130
|
+
"ref" => "development.profile.user_id",
|
131
|
+
"rows" => 1,
|
132
|
+
"filtered" => 100.0,
|
133
|
+
"Extra" => nil
|
134
|
+
}
|
135
|
+
}
|
136
|
+
```
|
137
|
+
|
138
|
+
### Detailed execution plan
|
139
|
+
|
140
|
+
Generated using `EXPAIN format=JSON <query>`
|
141
|
+
|
142
|
+
**Truncated Result**
|
143
|
+
|
144
|
+
```
|
145
|
+
{
|
146
|
+
"query_block": {
|
147
|
+
"select_id": 1,
|
148
|
+
"cost_info": {
|
149
|
+
"query_cost": "850.20"
|
150
|
+
},
|
151
|
+
"nested_loop": [
|
152
|
+
{
|
153
|
+
"table": {
|
154
|
+
"table_name": "profile",
|
155
|
+
"access_type": "index",
|
156
|
+
"key_length": "5",
|
157
|
+
"cost_info": {
|
158
|
+
"read_cost": "6.00",
|
159
|
+
"eval_cost": "120.60",
|
160
|
+
"prefix_cost": "126.60",
|
161
|
+
"data_read_per_join": "183K"
|
162
|
+
},
|
163
|
+
"used_columns": [
|
164
|
+
"user_id"
|
165
|
+
],
|
166
|
+
"attached_condition": "(`development`.`profile`.`user_id` is not null)"
|
167
|
+
}
|
168
|
+
}
|
169
|
+
]
|
170
|
+
}
|
171
|
+
}
|
172
|
+
```
|
173
|
+
|
174
|
+
|
175
|
+
Result for this is added in flatten form to final execution plan, where `detailed#` prefix is added before each key.
|
176
|
+
|
177
|
+
**Truncated Eg.**
|
178
|
+
|
179
|
+
```
|
180
|
+
{
|
181
|
+
"detailed#key_length" => "5",
|
182
|
+
"detailed#rows_examined_per_scan" => 603,
|
183
|
+
"detailed#rows_produced_per_join" => 603,
|
184
|
+
"detailed#filtered" => "100.00",
|
185
|
+
"detailed#using_index" => true,
|
186
|
+
"detailed#cost_info#read_cost" => "6.00",
|
187
|
+
"detailed#cost_info#eval_cost" => "120.60",
|
188
|
+
"detailed#cost_info#prefix_cost" => "126.60",
|
189
|
+
"detailed#cost_info#data_read_per_join" => "183K",
|
190
|
+
"detailed#used_columns" => ["user_id"]
|
191
|
+
...
|
192
|
+
```
|
193
|
+
|
194
|
+
|
195
|
+
|
196
|
+
##### Flatten
|
197
|
+
|
198
|
+
`{a: {b: 1}, c: 2}` is converted into `{a#b: 1, c: 2}`.
|
199
|
+
|
200
|
+
|
201
|
+
## Analysis object
|
202
|
+
|
203
|
+
Analysis object stores a detailed analysis report of a relation inside `:tables :table_count :summary attributes`.
|
204
|
+
|
205
|
+
#### Attributes
|
206
|
+
|
207
|
+
**table_count [Integer] - No. Tables used in the relation**
|
208
|
+
|
209
|
+
**tables [Hash] - detailed table analysis**
|
210
|
+
|
211
|
+
```
|
212
|
+
{
|
213
|
+
'users' => {
|
214
|
+
'id'=>1,
|
215
|
+
'name'=>'users', # table alias user in the execution plan
|
216
|
+
'analysis'=>{
|
217
|
+
'type'=>{ # attribute name
|
218
|
+
'value' => <string>, # raw value of attribute in execution plan
|
219
|
+
'tags' => {
|
220
|
+
'all' => { # tag based on the value of a attribute
|
221
|
+
'impact'=> <string>, # negative, positive, cautions
|
222
|
+
'warning'=> <string>, # Eg. 'warning to represent the issue'
|
223
|
+
'suggestions'=> <string> # Eg. 'some follow up suggestions'
|
224
|
+
}
|
225
|
+
}
|
226
|
+
}
|
227
|
+
}
|
228
|
+
}
|
229
|
+
}
|
230
|
+
```
|
231
|
+
**summary [Hash] - hash of analysis summary**
|
232
|
+
|
233
|
+
```
|
234
|
+
{
|
235
|
+
'cardinality'=>{
|
236
|
+
'amount'=>10,
|
237
|
+
'warning'=>'warning to represent the issue',
|
238
|
+
'suggestions'=>'some follow up suggestions'
|
239
|
+
}
|
240
|
+
}
|
241
|
+
```
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
## How to define rules?
|
246
|
+
|
247
|
+
Rules defined in the json file at config_path is applied to the execution plan. We have variety of option to define rules.
|
248
|
+
|
249
|
+
You can change this by `QueryPolice.rules_path=<path>` and define your own rules
|
250
|
+
|
251
|
+
### Rule Structure
|
252
|
+
|
253
|
+
A basic rule structure -
|
254
|
+
```
|
255
|
+
"<column_name>": {
|
256
|
+
"description": <string>,
|
257
|
+
"value_type": <string>,
|
258
|
+
"delimiter": <string>,
|
259
|
+
"rules": {
|
260
|
+
"<rule>": {
|
261
|
+
"amount": <integer>
|
262
|
+
"impact": <string>,
|
263
|
+
"message": <string>,
|
264
|
+
"suggestion": <string>
|
265
|
+
}
|
266
|
+
}
|
267
|
+
}
|
268
|
+
```
|
269
|
+
- `<column_name>` - attribute name in the final execution plan.
|
270
|
+
|
271
|
+
- `description` - description of the attribute
|
272
|
+
|
273
|
+
- `value_type` - value type of the attribute
|
274
|
+
|
275
|
+
- `delimiter` - delimiter to parse array type attribute values, if no delimiter is passed engine will consider value is already in array form.
|
276
|
+
|
277
|
+
- `<rule>` - kind of rule for the attribute
|
278
|
+
|
279
|
+
- `<tag>` - direct value match eg. ALL, SIMPLE
|
280
|
+
|
281
|
+
- `absent` - when value is missing
|
282
|
+
|
283
|
+
- `threshold` - a greater than threshold check based on the amount set inside the rule.
|
284
|
+
|
285
|
+
- `amount` - amount of threshold need to check for
|
286
|
+
|
287
|
+
- length for string
|
288
|
+
|
289
|
+
- value for number
|
290
|
+
|
291
|
+
- size for array
|
292
|
+
|
293
|
+
- `impact` - impact for the rule
|
294
|
+
|
295
|
+
- `negative`
|
296
|
+
|
297
|
+
- `postive`
|
298
|
+
|
299
|
+
- `caution`
|
300
|
+
|
301
|
+
- `message` - message need to provide the significance of the rule
|
302
|
+
|
303
|
+
- `suggestion` - suggestion on how we can fix the issue
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
### Dynamic messages and suggestion
|
308
|
+
|
309
|
+
We can define dynamic messages and suggestion with variables provided by the engine.
|
310
|
+
|
311
|
+
- `$amount` - amount of the value
|
312
|
+
|
313
|
+
- length for string
|
314
|
+
|
315
|
+
- value for number
|
316
|
+
|
317
|
+
- size for array
|
318
|
+
|
319
|
+
- `$column` - attribute name
|
320
|
+
|
321
|
+
- `$impact` - impact for the rule
|
322
|
+
|
323
|
+
- `$table` - table alias used in the plan
|
324
|
+
|
325
|
+
- `$tag` - tag for which rule is applied
|
326
|
+
|
327
|
+
- `$value` - original parsed value
|
328
|
+
|
329
|
+
- `$<column_name>` - value of that specific column in that table
|
330
|
+
|
331
|
+
- `$amount_<column_name>` - amount of that specific column
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
### Rules Examples
|
336
|
+
|
337
|
+
#### Basic rule example
|
338
|
+
|
339
|
+
```
|
340
|
+
"type": {
|
341
|
+
"description": "Join used in the query for a specific table.",
|
342
|
+
"value_type": "string",
|
343
|
+
"rules": {
|
344
|
+
"system": {
|
345
|
+
"impact": "positive",
|
346
|
+
"message": "Table has zero or one row, no change required.",
|
347
|
+
"suggestion": ""
|
348
|
+
},
|
349
|
+
"ALL": {
|
350
|
+
"impact": "negative",
|
351
|
+
"message": "Entire $table table is scanned to find matching rows, you have $amount_possible_keys possible keys to use.",
|
352
|
+
"suggestion": "Use index here. You can use index from possible key: $possible_keys or add new one to $table table as per the requirements."
|
353
|
+
}
|
354
|
+
}
|
355
|
+
```
|
356
|
+
For above rule dynamic message will be generated as-
|
357
|
+
```
|
358
|
+
Entire users table is scanned to find matching rows, you have 1 possible keys to use
|
359
|
+
```
|
360
|
+
For above rule dynamic suggestion will be generated as-
|
361
|
+
```
|
362
|
+
Use index here. You can use index from possible key: ["PRIMARY", "user_email"] or add new one to users table as per the requirements.
|
363
|
+
```
|
364
|
+
|
365
|
+
|
366
|
+
#### Absent rule example
|
367
|
+
|
368
|
+
```
|
369
|
+
"key": {
|
370
|
+
"description": "index key used for the table",
|
371
|
+
"value_type": "string",
|
372
|
+
"rules": {
|
373
|
+
"absent": {
|
374
|
+
"impact": "negative",
|
375
|
+
"message": "There is no index key used for $table table, and can result into full scan of the $table table",
|
376
|
+
"suggestion": "Please use index from possible_keys: $possible_keys or add new one to $table table as per the requirements."
|
377
|
+
}
|
378
|
+
}
|
379
|
+
}
|
380
|
+
```
|
381
|
+
|
382
|
+
For above rule dynamic message will be generated as-
|
383
|
+
```
|
384
|
+
There is no index key used for users table, and can result into full scan of the users table
|
385
|
+
```
|
386
|
+
For above rule dynamic suggestion will be generated as-
|
387
|
+
```
|
388
|
+
Please use index from possible_keys: ["PRIMARY", "user_email"] or add new one to users table as per the requirements.
|
389
|
+
```
|
390
|
+
|
391
|
+
|
392
|
+
#### Threshold rule example
|
393
|
+
|
394
|
+
```
|
395
|
+
"possible_keys": {
|
396
|
+
"description": "Index keys possible for a specifc table",
|
397
|
+
"value_type": "array",
|
398
|
+
"delimiter": ",",
|
399
|
+
"rules": {
|
400
|
+
"threshold": {
|
401
|
+
"amount": 5,
|
402
|
+
"impact": "negative",
|
403
|
+
"message": "There are $amount possible keys for $table table, having too many index keys can be unoptimal",
|
404
|
+
"suggestion": "Please check if there are extra indexes in $table table."
|
405
|
+
}
|
406
|
+
}
|
407
|
+
}
|
408
|
+
```
|
409
|
+
For above rule dynamic message will be generated as-
|
410
|
+
```
|
411
|
+
There are 10 possible keys for users table, having too many index keys can be unoptimal
|
412
|
+
```
|
413
|
+
For above rule dynamic suggestion will be generated as-
|
414
|
+
```
|
415
|
+
Please check if there are extra indexes in users table.
|
416
|
+
```
|
417
|
+
|
418
|
+
|
419
|
+
#### Complex Detailed rule example
|
420
|
+
|
421
|
+
```
|
422
|
+
"detailed#used_columns": {
|
423
|
+
"description": "",
|
424
|
+
"value_type": "array",
|
425
|
+
"rules": {
|
426
|
+
"threshold": {
|
427
|
+
"amount": 7,
|
428
|
+
"impact": "negative",
|
429
|
+
"message": "You have selected $amount columns, You should not select too many columns.",
|
430
|
+
"suggestion": "Please only select required columns."
|
431
|
+
}
|
432
|
+
}
|
433
|
+
}
|
434
|
+
```
|
435
|
+
For above rule dynamic message will be generated as-
|
436
|
+
```
|
437
|
+
You have selected 10 columns, You should not select too many columns.
|
438
|
+
```
|
439
|
+
For above rule dynamic suggestion will be generated as-
|
440
|
+
```
|
441
|
+
Please only select required columns.
|
442
|
+
```
|
443
|
+
|
444
|
+
|
445
|
+
### Summary
|
446
|
+
|
447
|
+
You can define similar rules for summary. Current summary attribute supported -
|
448
|
+
|
449
|
+
- `cardinality` - cardinality based on the all tables
|
450
|
+
|
451
|
+
**NOTE:** You can add custom summary attributes by defining how to calculate them in `QueryPolice.add_summary` for a attribute key.
|
452
|
+
|
453
|
+
|
454
|
+
|
455
|
+
### Attributes
|
456
|
+
|
457
|
+
There all lot of attributes for you to use based on the final execution plan.
|
458
|
+
|
459
|
+
You can use normal execution plan attribute directly.
|
460
|
+
Eg. `select_type, type, Extra, possible_keys`
|
461
|
+
|
462
|
+
To check more keys you can use `EXPLAIN <query>`
|
463
|
+
|
464
|
+
You can use detailed execution plan attribute can be used in flatten form with `detailed#` prefix.
|
465
|
+
Eg. `detailed#used_columns, detailed#cost_info#read_cost`
|
466
|
+
|
467
|
+
To check more keys you can use `EXPLAIN format=JSON <query>`
|
468
|
+
|
469
|
+
---
|
470
|
+
|
471
|
+
## Development
|
472
|
+
|
473
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
474
|
+
|
475
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
476
|
+
|
477
|
+
## Contributing
|
478
|
+
|
479
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/query_police. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/query_police/blob/master/CODE_OF_CONDUCT.md).
|
480
|
+
|
481
|
+
## License
|
482
|
+
|
483
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
484
|
+
|
485
|
+
## Code of Conduct
|
486
|
+
|
487
|
+
Everyone interacting in the QueryPolice project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/query_police/blob/master/CODE_OF_CONDUCT.md).
|