@@ -10,58 +10,73 @@ namespace QueryBuilder.Benchmarks;
1010[ MemoryDiagnoser ]
1111public class SelectsBenchmark
1212{
13- private Query query ;
13+ private Query selectSimple ;
14+ private Query selectGroupBy ;
15+ private Query selectWith ;
1416
1517 public Compiler compiler ;
1618
1719 [ Params (
18- EngineCodes . Firebird ,
19- EngineCodes . MySql ,
20- EngineCodes . Oracle ,
21- EngineCodes . PostgreSql ,
22- EngineCodes . Sqlite ,
2320 EngineCodes . SqlServer ) ]
2421 public string EngineCode { get ; set ; }
2522
2623 [ GlobalSetup ]
2724 public void Setup ( )
2825 {
29- query = new Query ( "Products" )
26+ selectSimple = new Query ( "Products" )
3027 . Select ( "ProductID" , "ProductName" , "SupplierID" , "CategoryID" , "UnitPrice" , "UnitsInStock" , "UnitsOnOrder" ,
3128 "ReorderLevel" , "Discontinued" )
32- . WhereContains ( "ProductName" , "Mascarpone" )
29+ . WhereIn ( "CategoryID" , [ 1 , 2 , 3 ] )
30+ . Where ( "SupplierID" , 5 )
3331 . Where ( "UnitPrice" , ">=" , 10 )
3432 . Where ( "UnitPrice" , "<=" , 100 )
3533 . Take ( 10 )
3634 . Skip ( 20 )
3735 . OrderBy ( "UnitPrice" , "ProductName" ) ;
36+
37+
38+ selectGroupBy = new Query ( "Products" )
39+ . Select ( "SupplierID" , "CategoryID" )
40+ . SelectAvg ( "UnitPrice" )
41+ . SelectMin ( "UnitPrice" )
42+ . SelectMax ( "UnitPrice" )
43+ . Where ( "CategoryID" , 123 )
44+ . GroupBy ( "SupplierID" , "CategoryID" )
45+ . HavingRaw ( "MIN(UnitPrice) >= ?" , 10 )
46+ . Take ( 10 )
47+ . Skip ( 20 )
48+ . OrderBy ( "SupplierID" , "CategoryID" ) ;
49+
50+ var activePosts = new Query ( "Comments" )
51+ . Select ( "PostId" )
52+ . SelectRaw ( "count(1) as Count" )
53+ . GroupBy ( "PostId" )
54+ . HavingRaw ( "count(1) > 100" ) ;
55+
56+ selectWith = new Query ( "Posts" )
57+ . With ( "ActivePosts" , activePosts )
58+ . Join ( "ActivePosts" , "ActivePosts.PostId" , "Posts.Id" )
59+ . Select ( "Posts.*" , "ActivePosts.Count" ) ;
60+
3861 compiler = TestSupport . CreateCompiler ( EngineCode ) ;
3962 }
4063
4164 [ Benchmark ]
42- public SqlResult Select ( )
65+ public SqlResult SelectSimple ( )
4366 {
44- return compiler . Compile ( query ) ;
67+ return compiler . Compile ( selectSimple ) ;
4568 }
4669
47- public static void Test ( )
70+ [ Benchmark ]
71+ public SqlResult SelectGroupBy ( )
72+ {
73+ return compiler . Compile ( selectGroupBy ) ;
74+ }
75+
76+ [ Benchmark ]
77+ public SqlResult SelectWith ( )
4878 {
49- var benchmark = new SelectsBenchmark ( ) ;
50- benchmark . EngineCode = EngineCodes . SqlServer ;
51- benchmark . Setup ( ) ;
52- var result = benchmark . Select ( ) . ToString ( ) ;
53- if ( result !=
54- Regex . Replace ( """
55- SELECT [ProductID], [ProductName], [SupplierID], [CategoryID], [UnitPrice], [UnitsInStock],
56- [UnitsOnOrder], [ReorderLevel], [Discontinued]
57- FROM [Products]
58- WHERE LOWER([ProductName]) like '%mascarpone%'
59- AND [UnitPrice] >= 10
60- AND [UnitPrice] <= 100
61- ORDER BY [UnitPrice], [ProductName] OFFSET 20 ROWS FETCH NEXT 10 ROWS ONLY
62- """ , @"\s+" , " " ) )
63- {
64- throw new ValidationException ( $ "Invalid result: { result } ") ;
65- }
79+ return compiler . Compile ( selectWith ) ;
6680 }
81+
6782}
0 commit comments