SQL automatic query analysis for involved tables/columns - sql

Let‘s say we have got a large number of SQL queries which take a long time to run. Now, we would like to make some changes to the database and re-execute the queries. We could rerun everything, but I would prefer a solution where only those queries are executed which are affected by the changes.
Do you know of any method to obtain the relevant tables/columns for each query? A simple example would be:
(let's consider this table: TABLE1 with columns: A;B;C)
SELECT C FROM TABLE1 WHERE B>10;
I would like to know that TABLE1.B is participating in this query.
Edit: the database is HSQLDB and is used from Java via JDBC.

Are you using any workbenck to execute your sql queries ? In Mysql workbench you have query optimizer option under which you can check which query has excuter and what actions has performed with the query result in a tree block diagram which certainly helps you here and you can also parse your query and check your resuls in query optimizer :) Hope it helps to you.

Related

very slow oracle sql fast after explain plan for

Hello i have SQL with few left joins and cases. Time on postgres is around one second and on oracle is 20minuts! (390 row postgres, 300 rows oracle). Configuration of tables are the same in both db. If i run in oracle EXPLAIN PLAN FOR for this sql first, then when i run SQL (without EPF) it is fast as on postgres. I can even make some changes, add colums etc and it still run nice and fast.
Is here someone who understand what is this and what solution i can use in java where query is used?

How I can find sql query for execution plan?

Some programm generate and send queries to sql server(on high load production). I want take plan of concrete query of concrete table. I start profiler with "Showplan XML" and set filter on TextData(like %MyTable%) and DatabaseName. It show rows with xml in TextData that describe execution plans(for all queries of my table). But I know that exist 5 different sql queries for this table.
How I can match some concrete query with correspond plan without use statistic?
Is there a reason this has to be done on the production environment? Most really bad execution plans (missing indexes causing table scans etc.) will be obvious enough on a dev environment where you can use all the diagnostics you want.
Otherwise running the SQL on the query cache (as in the linked question someone else mentioned) will probably have the lowest impact as it just queries a system table rather than adding diagnostics to every query.

Getting the schema of the query output in Hive

I am using Apache Hive to create and execute some queries, but before the query is executed I need to report the structure of the resultset. The queries may involve joins and projections so it will be quite difficult to parse the query. The current solution that we are working on involves parsing the output of explain command but its quite complex itself.
My question is whether there is some simpler way by setting some properties in hive or some query parameters that don't select any data (the map/reduce tasks are not started) but creates a table that I can query via metastore to get the schema?
Unfortunately there is no simpler way other than using EXPLAIN or DESCRIBE commands to get query schema and table schema.
While not something you can do before the query is returned, if you enable
set hive.cli.print.header=true;
then it will show the schema right before the results.

SQL View optimisation

I have inherited an existing system and am trying to figure out a few things.
The system does a
SELECT * FROM v_myView WHERE mvViewCol = 'someValue'
and v_myView performs summation of Table1 based on myViewCol
Does SQL Server 2005 optimize the query or will summation always occur across the entire Table1?
I understand that I could use a parameterized view but don't want to go changing things unnecessarily.
Cheers
Geoff
Views have no runtime cost at all. They are always inlined into the surrounding query as if you had pasted the view definition as text. They would be impractical to use otherwise.
Does SQL Server (2005) optimize the query or will summation always occur across the entire Table1.
It will be optimized.
This is a complicated question. I think the best explanation is here. I do wish Microsoft documentation were a little clearer on this point.
When a view is created, the query is parsed. This ensures that it is correct.
The execution plan is determined the first time the query is run (to a close approximation). This execution plan then remains in the plan cache for subsequent calls. So, if you have an index on the appropriate columns and the first execution has a where clause that would use the index, then subsequent calls will also use the index.
I say to a close approximation, because it is really the first time that a view is called when the plan is not in the plan cache. Certain changes to the database will flush the plan, as will restarting the server.
So, if you only access the view with the where clause, then subsequent uses of the view will be optimized for that purpose.
SQL Server 2005 will optimize the view each time it is referenced in a query : http://technet.microsoft.com/en-us/library/cc917715.aspx
"After view expansion, the SQL Server query optimizer compiles a single execution plan for the executing query."
I don't have 2005 installed but it will operate similiar to 2008R2 - To view the Query Optimization Plan, right click in the query window and select "Display Estimated Execution Plan" for more info and to spot any bottlenecks.
In the Query menu option, there is "Analyse Query in Database Tuning Advisor" that may also be of benefit to you.

Hibernate complex query

I am trying to execute a query against a MySQL database.
The query is fairly complex it has 5 inner joins, including 1 join to itself and
it returns 3 pieces of information from 2 different tables.
We are using hibernate and till now I have used it for simple queries only.
I have written the sql query and tested it too. I am wondering how to implement this using
hibernate, can I execute plain sql statements with hibernate? If so what do I need, a separate hbm.xml?
If I use hibernate and execute the plain sql query can I still utilize caching later on?
Yes, you can execute plain SQL queries with Hibernate.
No, you don't need a separate hbm.xml mapping file (unless you WANT to separate sql queries from the rest, in which case you can do so). You can map your named SQL query the same way you do with named HQL queries.
Whether you will be able to "utilize caching" depends on what exactly you understand by "caching" and how you're going to map your SQL query; it's impossible to answer without knowing more details.
All that said, you may not need to resort to SQL query; HQL is quite powerful and it may very well be possible (assuming appropriate mappings exist) to write your query as HQL. Can you post relevant mappings / schemas and your SQL query?
I strongly recommend criteria queries over HQL queries. They are much closer to your program code without sacrificing any expression power. They DO however depend on relations to be explicitly mapped, otherwise they get quite complicated.
To speed up development, set property hibernate.show_sql=true, and play with the system in the debugger, using the "reload modified class" and "drop stack frame" features of the IDE+jvm until the SQL emitted looks like the one you've posted.