question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Too aggressive query optimization

See original GitHub issue

Query like that:

db.Table1
    .Where(_ =>
        db.Table2
            .Where(r => r.Table1Id == _.Id)
            .OrderByDescending(r => r.SomeField)
            .Select(r => r.AssociationProperty.TextField)
            .First()
            .Contains(filterText))

Generates invalid subquery SQL without logic added by Select and Contains:

SELECT *
FROM Table1
WHERE
(
        SELECT TOP (1)
            *
        FROM
            Table2
        WHERE
            Table2.fk_id = table1.id
        ORDER BY
            Table2.SomeField DESC
)

Such query rejected by database engine (MSSQ) and miss join on association, text field selection and LIKE predicate

If you will need exact test-case to reproduce issue, I could provide it a bit later.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:12 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
MaceWinducommented, May 16, 2017

Of course.

Test query

[DataContextSource]
public void Issue269(string context)
{
	using (var db = new TestDataConnection(context))
	using (db.BeginTransaction())
	{
		var result = db.Patient
			.Where(pat => db.Person
				.Where(per => per.ID == pat.PersonID)
				.OrderByDescending(per => per.FirstName)
				.Select(c => c.Patient.Diagnosis)
				.First()
				.Contains("with"))
			.ToArray();
	}
}

Generated SQL (invalid). Similar to one in defect but a bit different. Maybe because of different relations schema but I assume it is due to changes made to linq2db for last two years.

SELECT
	[t1].[PersonID],
	[t1].[Diagnosis]
FROM
	[Patient] [t1]
WHERE
	1 = (
		SELECT TOP (1)
			*
		FROM
			[Person] [t2]
		WHERE
			[t2].[PersonID] = [t1].[PersonID]
		ORDER BY
			[t2].[FirstName] DESC
	)

Also commenting First call produce interesting results. Contains converted to comparison instead of LIKE:

SELECT
	[t1].[PersonID],
	[t1].[Diagnosis]
FROM
	[Patient] [t1]
WHERE
	EXISTS(
		SELECT
			*
		FROM
			[Person] [t3]
				LEFT JOIN [Patient] [t2] ON [t3].[PersonID] = [t2].[PersonID]
		WHERE
			[t3].[PersonID] = [t1].[PersonID] AND [t2].[Diagnosis] = N'with'
	)

0reactions
sdanylivcommented, May 19, 2017
  1. Maybe i have learn SQL before .NET, for me it is more explainable and really keeps you closer to SQL.
  2. I’m choosing both techniques, but for SQL I’m trying to use linq queries, especially when group by or join are used.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Query: SubQueryMemberPushDown optimization is too ...
Query : SubQueryMemberPushDown optimization is too aggressive, which may lead to invalid sql or data corruption #8505.
Read more >
Optimizer being overly aggressive
Optimizer being overly aggressive. Posted by: Adrian Cornish Date: July 06, 2012 08:18PM. I've reduced my issue down to this simple SP.
Read more >
SQL Server 2012 Aggressive Over-Indexing when i run ...
In my post How to Fix sp_BlitzIndex Aggressive Indexes Warnings, I note that ... Over-indexing means your table likely has too many indexes....
Read more >
How to Fix sp_BlitzIndex Aggressive Indexes Warnings
sp_BlitzIndex may report “Aggressive Indexes”, and point to the clustered index as being involved. However, that doesn't mean you need to DROP ...
Read more >
An overly aggressive optimization in BLASTN and ...
It appears the bug fix in BLAST+ 2.8.1 to remove the overly aggressive optimisation in BLASTN and MegaBLAST alone fixed 9 of the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found