DapperLib / DapperLib/Dapper

SqlMapper Query param empty list cause table scan in mysql

Open
#1,818 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
18.4k
Forks
3.7k
Avg merge
5h 8m
Merged PRs (30d)
1

Description

Previously reported in #1379, but the issue was closed by the original reporter for some reason, despite the actual problem still being present in Dapper.

connection.Query(
 "Select xxx from test WHERE messageid IN @messageids",
 new { messageids = new List() }
);

Generates the SQL Select xxx from test WHERE messageid IN (SELECT NULL WHERE 1 = 0), which causes a table scan in mysql due to poor query optimization.

@jayrowe's excellent investigation in the original issue shows that the table scan can be avoided by adding any FROM clause to the inner select, e.g. Select xxx from test WHERE messageid IN (SELECT NULL FROM (SELECT NULL) n WHERE 1 = 0)

Would you be open to a PR implementing this change?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No file or test is named. Start by tracing Dapper's handling of an empty list parameter for an IN clause, using the example and investigation in #1379; done means the generated MySQL SQL includes a FROM clause in the inner SELECT and avoids the reported table scan.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, mysql
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.