De acuerdo al video sobre las consultas más complejas y uso de joins y otras funciones como el group by u order by aporta mucho costo pero en donde estoy haciendo mis consultas sigue con el mismo numero de costo.
mysql> explain format=json SELECT p.codigo, YEAR(f.fecha) as anio, i.cantidad FROM tb_producto p INNER JOIN tb_item i ON i.codigo = p.codigo INNER JOIN tb_factura f ON f.numero = i.numero \G;
*************************** 1. row ***************************
EXPLAIN: {
"query_block": {
"select_id": 1,
"cost_info": {
"query_cost": "140304.75"
},
"nested_loop": [
{
"table": {
"table_name": "p",
"access_type": "index",
"possible_keys": [
"PRIMARY"
],
"key": "PRIMARY",
"used_key_parts": [
"codigo"
],
"key_length": "42",
"rows_examined_per_scan": 35,
"rows_produced_per_join": 35,
"filtered": "100.00",
"using_index": true,
"cost_info": {
"read_cost": "0.25",
"eval_cost": "3.50",
"prefix_cost": "3.75",
"data_read_per_join": "36K"
},
"used_columns": [
"codigo"
]
ERROR:
No query specified
mysql> explain format=json SELECT p.codigo, YEAR(f.fecha) as anio, SUM(i.cantidad) as cantida_sum FROM tb_producto p INNER JOIN tb_item i ON i.codigo = p.codigo INNER JOIN tb_factura f ON f.numero = i.numero GROUP BY p.codigo, YEAR(f.fecha) ORDER BY p.codigo, YEAR(f.fecha) \G;
*************************** 1. row ***************************
EXPLAIN: {
"query_block": {
"select_id": 1,
"cost_info": {
"query_cost": "140304.75"
},
"ordering_operation": {
"using_filesort": false,
"grouping_operation": {
"using_temporary_table": true,
"using_filesort": true,
"nested_loop": [
{
"table": {
"table_name": "p",
"access_type": "index",
"possible_keys": [
"PRIMARY"
],
"key": "PRIMARY",
"used_key_parts": [
"codigo"
],
"key_length": "42",
"rows_examined_per_scan": 35,
"rows_produced_per_join": 35,
"filtered": "100.00",
"using_index": true,
"cost_info": {
"read_cost": "0.25",
"eval_cost": "3.50",
"prefix_cost": "3.75",
"data_read_per_join": "36K"
},
"used_columns": [
"codigo"
]