I have a query that's generated by LINQ and generates a time out error.
But when I run the generated query in SQL server management studio it executes in less than one second.
Here's the query:
exec sp_executesql N'SELECT COUNT(*) AS [value]
FROM [dbo].[Document] AS [t0]
INNER JOIN [dbo].[Document_Search_order_nummer](#p0) AS [t1] ON [t0].[DocumentID] = [t1].[Key]
LEFT OUTER JOIN [dbo].[DocType] AS [t2] ON [t2].[Id] = [t0].[DocumentTypeIDOut]
LEFT OUTER JOIN [dbo].[DocTypeFormat] AS [t3] ON [t3].[Id] = [t2].[FormatId]
LEFT OUTER JOIN [dbo].[DocTypeType] AS [t4] ON [t4].[Id] = [t2].[TypeId]
INNER JOIN [dbo].[OriginalDocument] AS [t5] ON [t5].[OriginalDocID] = [t0].[OriginalDocID]
INNER JOIN [dbo].[User] AS [t6] ON [t6].[User_ID] = [t0].[DocumentFrom]
INNER JOIN [dbo].[User] AS [t7] ON [t7].[User_ID] = [t0].[DocumentTo]
WHERE ([t0].[DocumentID] <> #p1)',N'#p0 nvarchar(4000),#p1 int',#p0=N'"*+11110001+*" ',#p1=270675
Below is the log from SQL server profiler:
Using LINQ:
Eventclass= RPC:Completed
ApplicationName= .Net SqlClient Data Provider
CPU= 12625
Reads= 1137844
Writes = 0
Duration = 29989
Using SQL Server Management Studio:
Eventclass= SQL:BatchCompleted
ApplicationName= Microsoft SQL Server Management Studio - Query
CPU= 78
Reads= 31645
Writes = 0
Duration = 99
What's the cause of this big performance difference with an equal query and how can I solve this issue?
We had exactly the same problem once.
One possible cause could be the parameters type mismatching that causes this.
But, this wasn't the issue in our case. Turned out that in some cases the query just takes longer time to execute. So we isolated the query and ran it trough ADO.NET SqlCommand class. Believe it or not when we added extra white spaces between the sql commands caused the query to execute much faster.
So we took the query and created a view from it. It solved our problem.
Related
I have an SQL query with 2 subqueries. whenever I run it on MySQL Workbench on macOS, it gives "Error Code: 2013. Lost connection to MySQL server during query". However, when it runs on Workbench on Windows, it runs normally without any errors.
I tried to increase the connection timeout, but still no success!
Any clue on how to solve this issue?
I appreciate your support and cooperation.
here is a query that gives an error:
with t1 as(
SELECT s.name rep_name, r.name region_name, sum(o.total_amt_usd) as total_amt
FROM sales_reps s
JOIN accounts a
ON a.sales_rep_id = s.id
JOIN orders o
ON o.account_id = a.id
JOIN region r
ON r.id = s.region_id
group by 1,2),
t2 as(
select region_name, max(total_amt) as total_amt
from t1
group by 1)
select t1.rep_name, t1.region_name, t1.total_amt
from t1
join t2
ON t1.region_name = t2.region_name AND t1.total_amt = t2.total_amt;
Your query is taking too long to return data so the connection gets dropped. There are 2 ways to fix this issue.
(i) Optimize query
(ii) Increase MySQL timeout
Explaining 2nd way:
1. In the application menu, select Edit > Preferences > SQL Editor.
2. Look for the MySQL Session section and increase the DBMS connection read time out value.
3. Save the settings, quite MySQL Workbench and reopen the connection.
Finally, I uninstalled the workbench and installed it again and now it is working properly. Thanks for who tried to answer my questions.
I have a select statement running in a jsp against SQL Server (previously using MySql without issues).
the TOP 1 was added because otherwise SQL Server moans about order by clauses (but only when displaying a result in a jsp, not when running the query inside SQL Server Management Studio).
This query runs fine in SQL Server Management Studio
SELECT TOP 1
alerts.id,
alerts.ts,
asset_firstname,
asset_lastname,
assetid,
alerttype.name,
node.zonename,
node.ipaddress,
node.zonegroupid
from
alerts, asset, alerttype, node, alertrules
where
ack=0 and
alerts.nodeid = node.id and
alerts.alerttypeid = alerttype.id and
alertrules.alerttypeid = alerts.alerttypeid and
alerts.assetid = asset.id and
alerts.alerttypeid = 1 and
asset.id=1157 and
alertrules.userid = 1
order by alerts.ts desc
but, when run in the jsp it returns "Column alerts.ts is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause".
I don't want alerts.ts aggregated or grouped by, hence the 'correct' select statement.
If I remove TOP 1 or alerts.ts desc the query returns the wrong row (earliest rather than latest record)
Converting what should be straightforward basic SQL commands so they run properly with SQL Server is proving a nightmare.
Any thoughts appreciated.
Regards
Ralph
(I wrote this as an answer because as a comment would be a mess)
You are using old style joins, and have redundant checks. Maybe this could make a difference (not sure, as it seems to be a problem related to JSP):
SELECT TOP(1)
alerts.id, alerts.ts,
asset_firstname,
asset_lastname,
assetid,
alerttype.name,
node.zonename,
node.ipaddress,
node.zonegroupid
from alerts
inner join asset on alerts.assetid = asset.id
inner join alerttype on alerts.alerttypeid = alerttype.id
inner join node on alerts.nodeid = node.id
inner join alertrules on alertrules.alerttypeid = alerts.alerttypeid
where ack=0 and
alerts.alerttypeid = 1 and
asset.id=1157 and
alertrules.userid = 1
order by alerts.ts desc;
I have a query that is returning two different results from two MySql servers i'm running.
This is the query (generated by Entity Framework, I run it directly in MySql to ensure the results are coming back from each server differently):
SELECT
CASE WHEN Project1.C1 IS NULL THEN '5' ELSE Project1.data_entered END AS C1
FROM ( SELECT 1 AS X) AS SingleRowTable1
LEFT OUTER JOIN (SELECT
Extent1.data_entered,
1 AS C1
FROM rta_option_data AS Extent1
INNER JOIN rta_option_field AS Extent2 ON Extent1.rta_option_field_id = Extent2.rta_option_field_id
WHERE (Extent1.customer_id = 1546) AND (Extent2.Name = 'txtAdverseDelay') ) AS Project1 ON 1 = 1
Server A is running MySql 5.5.28 and returns 5 which is the correct and expected value.
Server B is running MySql 5.7.12-log and returns null which is the incorrect and unexpected value.
I'm at a complete loss of how to resolve this, I was planning a server upgrade this weekend but i'm super worried about other similar queries that could be impacted by this.
Additional notes:
They are both using the exact same data.
When I run the inner query on both servers they return the same results data_entered = null and C1 = null. This is the inner query i'm talking about:
SELECT
Extent1.data_entered,
1 AS C1
FROM rta_option_data AS Extent1
INNER JOIN rta_option_field AS Extent2 ON Extent1.rta_option_field_id = Extent2.rta_option_field_id
WHERE (Extent1.customer_id = 1546) AND (Extent2.Name = 'txtAdverseDelay')
Would appreciate any guidance/advice on how to possibly fix this - not sure if there is a setting in MySql or what.
I'm running an SQL Query on MS Access.
the query looks like this:
TRANSFORM MIN(X_VALUE*MULTIPLE & ' ' & Y_VALUE)
SELECT A.ID
FROM ((MY_TABLE_A A
INNER JOIN MY_TABLE_B B ON B.ID = A.ID)
INNER JOIN MY_TABLE_C C ON C.FOO1_ID = A.FOO1_ID)
LEFT JOIN MY_TABLE_D D ON A.FOO2_ID = D.FOO2_ID
WHERE A.NUM ='FOO'
AND A.FOO_ID<>0
AND FOO3=1
GROUP BY A.ID PIVOT X_NAME IN('BLAH1', 'BLAH2')
when running this against local MDB file, it works.
when running this against Linked MDB (tables are linked to remote Oracle DB), I'm getting
ERROR [42000] [Microsoft][ODBC Microsoft Access Driver] The Microsoft
Access database engine could not execute the SQL statement because it
contains a field that has an invalid data type.
I've googled it, and couldn't find anything useful.
Any idea what can I do?
thanks.
The only statement in the query that even vaguely seems it would cause data type issues is the mixed types in the transform statement. Perhaps the following would work:
TRANSFORM MIN(CSTR(X_VALUE*MULTIPLE) & ' ' & CSTR(Y_VALUE))
I wrote a query which is fetching 60000 rows and creating excel file which should be available for download as excel file, but every time I am getting some errors like '500 internal server error' or 'mysql server has gone away'. But it works fine for lesser rows(10-15k) Here is mysql query:
SELECT C.*, T.Name AS TankName, T.*, P.*
FROM properties P
LEFT JOIN contacts C ON C.ID = P.ContactID AND C.CompanyID = $companyID
LEFT JOIN tanks T on T.PropertyID = P.ID $subQuery
WHERE P.CompanyID = $companyID
$condition
ORDER BY C.FullName = '', C.LastName, P.City, P.Address, P.ID
Do I need to optimize the query or change some sql configuration.
Try increasing your my.cnf variables...
If this is innodb table or myisam table, according to this increase variables value in my.cnf.
Either memory is used up and server terminates the query or restarts mysql...
what error log give in this query ?
Thanks