Skip to content

Commit

Permalink
Merge pull request #4 from macbre/index-digest-sql-log
Browse files Browse the repository at this point in the history
Add "index-digest-sql-log" option
  • Loading branch information
macbre authored Feb 8, 2021
2 parents 01d5925 + 9c14dc0 commit 4a52cb5
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 4 deletions.
12 changes: 11 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,26 @@ jobs:
echo -e "[client]\nuser=digest\npassword=s3cr3t\n" > ~/.my.cnf
mysql --host=127.0.0.1 --port=3310 --user=digest --database=index_digest < ./sql/0001_redundant_indices.sql
mysql --host=127.0.0.1 --port=3310 --user=digest --database=index_digest < ./sql/0002_queries_not_using_indices.sql
mysql --host=127.0.0.1 --port=3310 --user=digest --database=index_digest --execute="SHOW TABLES"
# docker run --network=host -t macbre/index-digest:latest mysql://index_digest:qwerty@debian/index_digest | head -n 20
cp ./sql/log.sql /tmp/log.sql
- name: Run an action
uses: ./
with:
index-digest-version: ${{ matrix.index-digest-version }}
index-digest-dsn: "mysql://digest:s3cr3t@127.0.0.1:3310/index_digest"
index-digest-report-file: "./report.yml"

- name: Run an action (with --sql-log)
uses: ./
with:
index-digest-version: ${{ matrix.index-digest-version }}
index-digest-dsn: "mysql://digest:s3cr3t@127.0.0.1:3310/index_digest"
index-digest-sql-log: "/tmp/log.sql" # use an absolute path here!
index-digest-report-file: "./report.yml"

- name: Check the version and the output file
run: |
docker run -t macbre/index-digest:${{ matrix.index-digest-version }} --version
Expand Down
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,19 @@ You will then have **all queries** (not only slow ones - see `--long-query-time`
Then, having a slow query log, all you need to do is parse it with [`pt-query-digest`](https://www.percona.com/doc/percona-toolkit/LATEST/pt-query-digest.html) and get the list of unique SQL queries:

```
pt-query-digest /tmp/log/slow_query.log --output json | jq .classes[].example.query | sed 's/\\n/ /g'| jq -r . > log.sql
pt-query-digest /tmp/log/slow_query.log --output json | jq .classes[].example.query | sed 's/\\n/ /g'| jq -r . > /tmp/log.sql
```

And then run the action:

```yaml
- name: Install and run index-digest
uses: macbre/actions-index-digest@0.3.0
with:
index-digest-version: "1.4.0"
index-digest-dsn: "mysql://test_user:test_password@127.0.0.1:3306/test_db"
index-digest-sql-log: "/tmp/log.sql" # use an absolute path here!
index-digest-report-file: "./report.yml"
```

> Please note that `index-digest-sql-log` **needs to be provided with an absolute path**.
17 changes: 15 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ inputs:
description: "DSN pointing at the database to check"
require: true

index-digest-sql-log:
description: "File with SQL queries to check against the database"
require: false
default: "/tmp/index_digest_empty_log.sql"

index-digest-checks:
description: "Which index-digest checks to perform"
require: true
Expand All @@ -31,12 +36,20 @@ runs:
# https://hub.docker.com/r/macbre/index-digest
- shell: bash
run: |
echo "Using index-version v${{ inputs.index-digest-version }} ..."
echo "Using index-version ${{ inputs.index-digest-version }} ..."
docker pull macbre/index-digest:${{ inputs.index-digest-version }}
echo "Using SQL log file: ${{ inputs.index-digest-sql-log }} ..."
# create an empty SQL log file (will be used when "index-digest-sql-log" is not provided)
touch /tmp/index_digest_empty_log.sql
- shell: bash
run: |
docker run --network=host -t macbre/index-digest:${{ inputs.index-digest-version }} \
docker run --network=host --volume="${{ inputs.index-digest-sql-log }}:/tmp/log.sql:ro" \
-t macbre/index-digest:${{ inputs.index-digest-version }} \
--checks=${{ inputs.index-digest-checks }} \
--sql-log=/tmp/log.sql \
--format=yaml \
${{ inputs.index-digest-dsn }} | tee ${{ inputs.index-digest-report-file }}
12 changes: 12 additions & 0 deletions sql/0002_queries_not_using_indices.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CREATE TABLE `0002_queries_not_using_indices` (
`item_id` int(9) NOT NULL AUTO_INCREMENT,
`foo` varchar(16) NOT NULL DEFAULT '',
`bar` varchar(16) NOT NULL DEFAULT '',
PRIMARY KEY (`item_id`),
KEY `bar_idx` (`bar`)
);

INSERT INTO 0002_queries_not_using_indices VALUES
(1, 'test', ''),
(2, 'foo', 'test'),
(3, 'foo', 'check');
13 changes: 13 additions & 0 deletions sql/log.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- these use index
SELECT item_id FROM 0002_queries_not_using_indices WHERE item_id = 2;
SELECT item_id FROM 0002_queries_not_using_indices WHERE item_id BETWEEN 1 AND 3;
SELECT item_id FROM 0002_queries_not_using_indices WHERE foo = "test" AND item_id = 1;
-- these do not use index
SELECT item_id FROM 0002_queries_not_using_indices WHERE foo = "test" OR item_id > 1;
SELECT item_id FROM 0002_queries_not_using_indices WHERE foo = "test"
-- no matching row in const table (#44)
SELECT foo FROM 0002_queries_not_using_indices WHERE item_id = 5;
-- #148: EXPLAINS' Extra says "No tables used"
SELECT 1*1;
SELECT 1 AS one FROM dual WHERE exists ( SELECT item_id FROM 0002_queries_not_using_indices WHERE foo = "test" );

0 comments on commit 4a52cb5

Please sign in to comment.