Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
extend the use of run_and_show
modify a method to get counts of query updating same PK

Signed-off-by: Frederic Descamps <lefred.descamps@gmail.com>
  • Loading branch information
lefred committed Jul 3, 2020
1 parent acd4590 commit 813421d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 47 deletions.
70 changes: 25 additions & 45 deletions check/queries.py
@@ -1,5 +1,8 @@
from ext.mysqlsh_plugins_common import run_and_show

def _get_full_details(shell, session, original_query, schema):
def _get_full_details(session, original_query, schema):
import mysqlsh
shell = mysqlsh.globals.shell
old_schema=None
if session.get_current_schema() is None:
old_schema=None
Expand All @@ -10,23 +13,19 @@ def _get_full_details(shell, session, original_query, schema):
answer = shell.prompt('Do you want to have EXPLAIN output? (y/N) ', {'defaultValue':'n'})
if answer.lower() == 'y':
stmt = """EXPLAIN %s""" % original_query
result = session.run_sql(stmt)
shell.dump_rows(result,'vertical')
run_and_show(stmt,'vertical')
answer = shell.prompt('Do you want to have EXPLAIN in JSON format output? (y/N) ', {'defaultValue':'n'})
if answer.lower() == 'y':
stmt = """EXPLAIN FORMAT=json %s""" % original_query
result = session.run_sql(stmt)
shell.dump_rows(result,'vertical')
run_and_show(stmt,'vertical')
answer = shell.prompt('Do you want to have EXPLAIN in TREE format output? (y/N) ', {'defaultValue':'n'})
if answer.lower() == 'y':
stmt = """EXPLAIN format=tree %s""" % original_query
result = session.run_sql(stmt)
shell.dump_rows(result,'vertical')
run_and_show(stmt,'vertical')
answer = shell.prompt('Do you want to have EXPLAIN ANALYZE output? (y/N) ', {'defaultValue':'n'})
if answer.lower() == 'y':
stmt = """EXPLAIN ANALYZE %s""" % original_query
result = session.run_sql(stmt)
shell.dump_rows(result,'vertical')
stmt = """EXPLAIN ANALYZE %s""" % original_query
run_and_show(stmt,'vertical')
if old_schema:
session.set_current_schema(old_schema)
return
Expand All @@ -42,7 +41,6 @@ def get_queries_95_perc(limit=1, select=False, schema=None, session=None):
print("No session specified. Either pass a session object to this "
"function or connect the shell to a database")
return

filter = ""
if select:
filter += "AND query_sample_text like '%select%'"
Expand All @@ -60,15 +58,14 @@ def get_queries_95_perc(limit=1, select=False, schema=None, session=None):
ORDER BY (total_latency/exec_count) desc
LIMIT %d""" % (filter, limit)

result = session.run_sql(stmt)
shell.dump_rows(result,'vertical')
run_and_show(stmt,'vertical')

if limit == 1:
result = session.run_sql(stmt)
row = result.fetch_one()
if row:
original_query = row[6]
_get_full_details(shell, session, original_query, row[0])
_get_full_details(session, original_query, row[0])

def get_queries_ft_scan(limit=1, select=False, schema=None, session=None):

Expand Down Expand Up @@ -101,8 +98,7 @@ def get_queries_ft_scan(limit=1, select=False, schema=None, session=None):
ORDER BY (total_latency/exec_count) desc
LIMIT %d""" % (filter, limit)

result = session.run_sql(stmt)
shell.dump_rows(result,'vertical')
run_and_show(stmt,'vertical')

if limit == 1:
result = session.run_sql(stmt)
Expand Down Expand Up @@ -140,8 +136,7 @@ def get_queries_temp_disk(limit=1, schema=None, session=None):
ORDER BY (total_latency/exec_count) desc
LIMIT %d""" % (filter, limit)

result = session.run_sql(stmt)
shell.dump_rows(result,'vertical')
run_and_show(stmt,'vertical')

if limit == 1:
result = session.run_sql(stmt)
Expand All @@ -152,16 +147,6 @@ def get_queries_temp_disk(limit=1, schema=None, session=None):

def get_queries_most_rows_affected(limit=1, schema=None, session=None):

import mysqlsh
shell = mysqlsh.globals.shell

if session is None:
session = shell.get_session()
if session is None:
print("No session specified. Either pass a session object to this "
"function or connect the shell to a database")
return

filter = ""
if schema is not None:
filter += "where db = '%s'" % schema
Expand All @@ -173,30 +158,25 @@ def get_queries_most_rows_affected(limit=1, schema=None, session=None):
ON ed.digest=sa.digest %s
ORDER BY rows_affected_avg DESC, rows_affected DESC LIMIT %s
""" % (filter, limit)
result = session.run_sql(stmt)
shell.dump_rows(result,'vertical')

def get_queries_updating_same_pk(limit=1, schema=None, session=None):

import mysqlsh
shell = mysqlsh.globals.shell
run_and_show(stmt,'vertical')

if session is None:
session = shell.get_session()
if session is None:
print("No session specified. Either pass a session object to this "
"function or connect the shell to a database")
return
def get_queries_updating_same_pk(limit=1, schema=None, session=None):

filter = ""
if schema is not None:
filter += "where (current_schema = '%s' or object_schema = '%s')" % (schema, schema)


stmt = """SELECT current_schema, rows_examined, SQL_TEXT
FROM performance_schema.events_statements_history_long
stmt = """SELECT current_schema, rows_examined, sql_text,
(
SELECT count(*)
FROM performance_schema.events_statements_history_long AS t2
WHERE t2.digest_text=t1.digest_text
) AS `count`
FROM performance_schema.events_statements_history_long AS t1
WHERE rows_affected > 1 %s
ORDER BY timer_wait DESC LIMIT %s
""" % (filter, limit)
result = session.run_sql(stmt)
shell.dump_rows(result,'vertical')

run_and_show(stmt,'vertical')
4 changes: 2 additions & 2 deletions mysqlsh_plugins_common.py
Expand Up @@ -126,7 +126,7 @@ def are_instruments_enabled(instrument_name, session, shell):

return ok

def run_and_show(stmt, session=None):
def run_and_show(stmt, format='table',session=None):
import mysqlsh
shell = mysqlsh.globals.shell

Expand All @@ -138,5 +138,5 @@ def run_and_show(stmt, session=None):
return

result = session.run_sql(stmt)
shell.dump_rows(result)
shell.dump_rows(result, format)
return

0 comments on commit 813421d

Please sign in to comment.