Why did you sql query overcome hibernate query? - mysql

I want to know difference between Mysql Query and Hibernate Query. Anybody know give your Suggestion

There is a world of difference between the two. I will try my best to explain it to you.
For writing a MySQL query, you need to think in terms of tables, whereas for a Hibernate query, you need to think in terms of objects.
If you have MySQL queries embedded in your Java code, when you try to switch your database to a different one (say Oracle for example), your queries may not work anymore. This is because different db vendors have different syntax's that they need you to use to accomplish the same goal.
However, in the case of a Hibernate query, you will need to just change the appropriate property in the hibernate configuration file. Since you write queries in terms of objects, Hibernate will automatically generate the appropriate SQL it requires to work with the underlying db.
Also, one another major difference between the two (or between Hibernate and a specific db query language) is the way joins are done. We can use the dot operator in Hibernate Query Language (HQL) to access the properties of a component object without needing to explicitly specify a JOIN clause as we would in a specific query language.
Besides this, there are tons of differences between the two and in no way can all of them be summarized here.

Related

Cheapest SQL Statement possible / Are there Client-Side SQL Statements?

Questions
What is/are the most cheapest SQL-Statment(s) (in terms of Processing Overhead/CPU Cycles).
Are there (this will most likely be DB-Client specific) any Statments that are evaluated directly by the client and even do not go to the database server?
The result doesn't matter, if an empty statement (which produces an SQL Error) is the cheapest OK, then this is good too. But I am more interested in non Error Responses.
Background:
I have an application that queries a lot of data from the DB. However I do not require this data. Sadly, I have no possibility to skip this query. But I have the possibility to change the SQL Query itself. So I am trying to find the cheapst SQL Statement to use, ideally it should not even go to the SQL Server and the SQL-Client Library should answer it. I will be using MySQL.
UPDATES (on comments):
Yes, it can be a No-Operation. It must be something I can pass as a regular SQL String to the mysql client library. Whatever that string could be, is the question. The goal is, that this Query then somehowreturns nothing, using the least Resources on the SQL Server as possible. But in idealcase the client itself will realize that this query doesnt even have to go to the server, like a version Check of the client library (OK I know this is no standard SQL then but maybe there is something I do not know about, a statement that will be "short circuited/answered" on the client itself).
Thanks very much!
DO 0
DO executes the expressions but does not return any results. In most respects, DO is shorthand for SELECT expr, ..., but has the advantage that it is slightly faster when you do not care about the result.

SQL Server Object Dependencies

Red Gate has some pretty good tools, but I don't think that their Dependency Tracker shows how Tables are effected by the stored procedures that touch them.
Is there any tool that can scan a database and determine what processes INSERT, UPDATE or DELETE records from the table as opposed to just touching\being dependent on them? Seems like this shuld exist by now...
No, dependency tracking still isn't perfect. The reason is that procedures can reference tables by dynamic SQL, dependencies can be broken if objects are dropped and re-created (I've written about how dependencies can break here). The best "first sweep" I have come to rely on is:
SELECT OBJECT_NAME([object_id])
FROM sys.sql_modules
WHERE LOWER(definition) LIKE '%table_name%';
Again, this won't find objects that build statements using dynamic SQL, and it can produce false positives because table_name could be simplistic and part of other object or parameter names, or included only in comments or commented-out code.
You can also check for plans that reference a table using sys.dm_exec_cached_plans and related DMFs/DMVs but note that this won't find any plans that have rolled out of the cache.
Using SQL Search, you can search for the column name and find all the stored procedures where it is used.
It's a Third Party tool and that is Red Gate SQL Search
Features
Find fragments of SQL text within stored procedures, functions, views
and more
Quickly navigate to objects wherever they happen to be on a server
Find all references to an object
Hope this will help you.

What is the best way to filter a multi-tenant MySQL database?

In MySQL I have a single database with one schema. In Microsoft Sql Server it is recommended to use a "Tenant View Filter" so in Microsoft Sql Server this gives me exactly what I need.
CREATE VIEW TenantEmployees AS
SELECT * FROM Employees WHERE TenantID = SUSER_SID()
What is the best way to accomplish the same in MySQL? An equivalent to the "Tenant View Filter" will work if it is performs well.
Thanks!!
The query you suggest (that I could find in MSDN) has text afterwards that explains exactly what are its assumptions. In particular, it mentions that it assumes that the "owner" of a row in the Employees table is specified in the TenantID field that is populated according to the SID of the user(s) you are partitioning for.
What that means is that you can replicate the same idea whatever way you decide to implement your data as long as you have clearly defined partitions of the data and know exactly how to associate it with the table you are creating a view for.
In particular, if you configure your system so that each partition accesses the DB with its own credentials, you could use the CURRENT_USER or USER constructs of MySQL as the IDs defining your partitions and the query to create the view would be basically the same as the one suggested in MSDN replacing SUSER_ID with CURRENT_USER.
But if you use the same user to access from all the partitions, then the suggested method is irrelevant on either database server.
Since you need to use your tenantId value to perform filtering, a table valued user defined function would be ideal, as a view normally does not accept parameters. Unfortunately, unlike many other database products MySQL doesn't support table-valued functions. However, there are MySQL hacks that claim to emulate parametrized views. These could be useful for you.
It's a little tricky in MySQL, but it can be done:
CREATE OR REPLACE VIEW {viewName}
AS
SELECT {fieldListWithoutTenantID}
FROM {tableName}
WHERE (id_tenant = SUBSTRING_INDEX(USER( ),'#',1))
I wrote up a full blog post on how I converted a single-tenant MySQL application to multi-tenant in one weekend with minimal changes. https://opensource.io/it/mysql-multi-tenant/

LINQ to SQL - Two tables, same name? or Alternate DB definition?

I need to update a C# application that imports data into a database using LINQ. I am new to LINQ. The problem I am trying to solve is that there are two versions of the DB. They have the same table names and are 90% identical in structure, but have one table (out of about 60) which has a different definition.
If LINQ were not involved, I would simply select a different query depending on which version of the application (DB) the user wanted to import to, and leave the remainder of the application as is.
My impression is that LINQ is intended for situations in which the DB structure is cast in stone, and that I cannot have two LINQ table definitions having the same name and simply or easily switch between them (or do so at all).
In this case, must I have (at least) a separate entire Linq.DataContext for each version of the DB? Or have I misunderstood something basic here?
You might be able to make that happen using separate mappings. In this case you would have to hand code your mappings as apposed to the attribute-based mapping that the LINQ designer or SqlMetal does for you. I've never done it, but I think it might work. I just googled for "Linq to Sql POCO mapping" and found this: Achieving POCO s in Linq to SQL. This person is loading his mapping from an xml file at runtime. You could conditionally load one of two different mapping files.

Dynamic Linq - query a schema that is only known at run time?

I know with dynamic linq you can construct expressions dynamically in the same way that you might build and execute a dynamic SQL statement - e.g. a dynamic where clause or a dynamic select list. Is it possible to do this in cases where the schema is not known at compile time?
In a database I'm working with users can define their own entities which causes new tables/columns to be created in the back-end database. At run time I'll know the table & column names I need to work with but I won't know the schema at compile time hence I can't build a DBML to work with up front.
Is there any facility for the dynamic discovery of the schema at run time or is this a case where I need to stick with building dynamic SQL statements?
As far as we understand, you don't know neither schema name nor the full structure of your schema for sure.
In this case it seems that the strongly-typed ExecuteQuery method overload will be an option.
Just write the SQL queries and add the necessary parameters (like table and column names) either using string concatenation or as parameters.