Skip to content

Commit 95c3019

Browse files
committed
Added complex SQL
1 parent 9ce0701 commit 95c3019

1 file changed

Lines changed: 80 additions & 16 deletions

File tree

‎test/benchmark/SqlBuildBench.php‎

Lines changed: 80 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PhpBench\Attributes\Revs;
1111
use PhpBench\Attributes\Warmup;
1212
use PhpDb\Adapter\Platform\Sql92;
13+
use PhpDb\Sql\Expression;
1314
use PhpDb\Sql\Select;
1415
use Symfony\Component\Cache\Adapter\ArrayAdapter;
1516
use Symfony\Component\Cache\Psr16Cache;
@@ -63,8 +64,38 @@ private function createSelectWithMultipleJoins(): Select
6364
return $select;
6465
}
6566

66-
#[Revs(1000)]
67-
#[Iterations(10)]
67+
private function createVeryComplexSelect(): Select
68+
{
69+
// Subquery for total order amount per user
70+
$orderTotalSubquery = new Select('orders');
71+
$orderTotalSubquery->columns(['total_amount' => new Expression('SUM(total)')])
72+
->where('orders.user_id = users.id');
73+
74+
$select = new Select('users');
75+
$select->columns([
76+
'id',
77+
'name',
78+
'email',
79+
'status',
80+
'created_at',
81+
'order_total' => $orderTotalSubquery,
82+
'order_count' => new Expression('COUNT(DISTINCT orders.id)'),
83+
])
84+
->join('orders', 'orders.user_id = users.id', [])
85+
->join('order_items', 'order_items.order_id = orders.id', [])
86+
->join('products', 'products.id = order_items.product_id', [])
87+
->join('categories', 'categories.id = products.category_id', [])
88+
->where('users.status IS NOT NULL')
89+
->where('users.created_at > 0')
90+
->group(['users.id', 'users.name', 'users.email', 'users.status', 'users.created_at'])
91+
->having('COUNT(DISTINCT orders.id) > 0')
92+
->order(['order_count DESC', 'users.name ASC']);
93+
94+
return $select;
95+
}
96+
97+
#[Revs(2000)]
98+
#[Iterations(20)]
6899
#[Warmup(2)]
69100
#[BeforeMethods('setUp')]
70101
public function benchSimpleSelectWithoutCache(): void
@@ -73,8 +104,8 @@ public function benchSimpleSelectWithoutCache(): void
73104
$select->getSqlString($this->platformWithoutCache);
74105
}
75106

76-
#[Revs(1000)]
77-
#[Iterations(10)]
107+
#[Revs(2000)]
108+
#[Iterations(20)]
78109
#[Warmup(2)]
79110
#[BeforeMethods('setUp')]
80111
public function benchSimpleSelectWithCache(): void
@@ -83,8 +114,8 @@ public function benchSimpleSelectWithCache(): void
83114
$select->getSqlString($this->platformWithCache);
84115
}
85116

86-
#[Revs(1000)]
87-
#[Iterations(10)]
117+
#[Revs(2000)]
118+
#[Iterations(20)]
88119
#[Warmup(2)]
89120
#[BeforeMethods('setUp')]
90121
public function benchSimpleSelectWithCacheColdStart(): void
@@ -96,8 +127,8 @@ public function benchSimpleSelectWithCacheColdStart(): void
96127
$select->getSqlString($platform);
97128
}
98129

99-
#[Revs(1000)]
100-
#[Iterations(10)]
130+
#[Revs(2000)]
131+
#[Iterations(20)]
101132
#[Warmup(2)]
102133
#[BeforeMethods('setUp')]
103134
public function benchComplexSelectWithoutCache(): void
@@ -106,8 +137,8 @@ public function benchComplexSelectWithoutCache(): void
106137
$select->getSqlString($this->platformWithoutCache);
107138
}
108139

109-
#[Revs(1000)]
110-
#[Iterations(10)]
140+
#[Revs(2000)]
141+
#[Iterations(20)]
111142
#[Warmup(2)]
112143
#[BeforeMethods('setUp')]
113144
public function benchComplexSelectWithCache(): void
@@ -116,8 +147,8 @@ public function benchComplexSelectWithCache(): void
116147
$select->getSqlString($this->platformWithCache);
117148
}
118149

119-
#[Revs(1000)]
120-
#[Iterations(10)]
150+
#[Revs(2000)]
151+
#[Iterations(20)]
121152
#[Warmup(2)]
122153
#[BeforeMethods('setUp')]
123154
public function benchComplexSelectWithCacheColdStart(): void
@@ -129,8 +160,8 @@ public function benchComplexSelectWithCacheColdStart(): void
129160
$select->getSqlString($platform);
130161
}
131162

132-
#[Revs(1000)]
133-
#[Iterations(10)]
163+
#[Revs(2000)]
164+
#[Iterations(20)]
134165
#[Warmup(2)]
135166
#[BeforeMethods('setUp')]
136167
public function benchMultiJoinSelectWithoutCache(): void
@@ -139,13 +170,46 @@ public function benchMultiJoinSelectWithoutCache(): void
139170
$select->getSqlString($this->platformWithoutCache);
140171
}
141172

142-
#[Revs(1000)]
143-
#[Iterations(10)]
173+
#[Revs(2000)]
174+
#[Iterations(20)]
144175
#[Warmup(2)]
145176
#[BeforeMethods('setUp')]
146177
public function benchMultiJoinSelectWithCache(): void
147178
{
148179
$select = $this->createSelectWithMultipleJoins();
149180
$select->getSqlString($this->platformWithCache);
150181
}
182+
183+
#[Revs(2000)]
184+
#[Iterations(20)]
185+
#[Warmup(2)]
186+
#[BeforeMethods('setUp')]
187+
public function benchVeryComplexSelectWithoutCache(): void
188+
{
189+
$select = $this->createVeryComplexSelect();
190+
$select->getSqlString($this->platformWithoutCache);
191+
}
192+
193+
#[Revs(2000)]
194+
#[Iterations(20)]
195+
#[Warmup(2)]
196+
#[BeforeMethods('setUp')]
197+
public function benchVeryComplexSelectWithCache(): void
198+
{
199+
$select = $this->createVeryComplexSelect();
200+
$select->getSqlString($this->platformWithCache);
201+
}
202+
203+
#[Revs(2000)]
204+
#[Iterations(20)]
205+
#[Warmup(2)]
206+
#[BeforeMethods('setUp')]
207+
public function benchVeryComplexSelectWithCacheColdStart(): void
208+
{
209+
$platform = new Sql92();
210+
$platform->setCache(new Psr16Cache(new ArrayAdapter()));
211+
212+
$select = $this->createVeryComplexSelect();
213+
$select->getSqlString($platform);
214+
}
151215
}

0 commit comments

Comments
 (0)