Closed splio-aleroux closed 7 years ago
Would be nice to understand what it does without trying it out. What I miss, is a description, and documentation for the website.
Where is the 'employee' in the example refer too? I assume it's another entity, or table.
Let's say i have a table as follows:
CREATE TABLE `my_table` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`is_valid` int(1) DEFAULT NULL,
`type` varchar(8) DEFAULT NULL,
`quantity` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `index_is_valid` (`is_valid`),
KEY `index_type` (`type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
For different use case, i need to get either rows with constraints on is_valid
, on type
or both:
SELECT SUM(`my_table`.`quantity`) FROM `my_table` WHERE `my_table`.`is_valid` = 1 and `my_table`.`type` = "orange"
or
SELECT SUM(`my_table`.`quantity`) FROM `my_table` WHERE `my_table`.`is_valid` = 1
or
SELECT SUM(`my_table`.`quantity`) FROM `my_table` WHERE `my_table`.`type` = "virtual"
So, i would like to setup an index for both is_valid
and type
in addition to the existing indexes for performances considerations:
CREATE TABLE `my_table` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`is_valid` int(1) DEFAULT NULL,
`type` varchar(8) DEFAULT NULL,
`quantity` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `index_is_valid_type` (`is_valid`,`type`),
KEY `index_is_valid` (`is_valid`),
KEY `index_type` (`type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
This PR allows such setup by defining the entity's fields as follows:
// myTable.php
public static function fields()
{
return [
'id' => [
'type' => 'integer',
'primary' => true
],
'is_valid' => [
'type' => 'bool',
'required' => false,
'index' => [
true,
'is_valid_type'
]
],
'type' => [
'type' => 'string',
'length' => '8',
'required' => false,
'index' => [
true,
'is_valid_type'
]
],
'quantity' => [
'type' => 'integer',
'required' => false,
'index' => true
],
];
}
I hope this is easier to understand now.
This is a far better explanation (for sure because you explained also what it would be in plain SQL) If this is merged, we should definitely add this to the normal documentation as well.
Any news here ?
Yeah - sorry. I love this feature. Great job. 👍
Thanks !
This PR slightly modifies the 'index' argument of a field definition. It allows to define compound index on fields that are already in other index scope.
usage :
tests (mysql):