SQL 语句在 laravel 中执行需要 26 秒,在 phpmyadmin 中需要

我有一个涉及 6 个表的 sql 语句,在 laravel 中执行它需要 26 秒,但相同的 sql 语句在 phpmyadmin 中大约需要 0.0075 到 .0089 秒。

http://img2.mukewang.com/643a6a9c00017b6904920117.jpg

http://img.mukewang.com/643a6aa300011fcf01180083.jpg

这是我在 laravel 中的代码:


use DB;    

$partidas = DB::select("SELECT A.Patente, A.Pedimento, A.SeccionAduanera, A.Fraccion, A.SecuenciaFraccion, A.ValorComercial, A.PrecioUnitario, A.CantidadUMComercial, A.UnidadMedidaComercial, A.CantidadUMTarifa, A.UnidadMedidaTarifa, A.MetodoValorizacion, A.PaisOrigenDestino, A.PaisCompradorVendedor,  

                                                B.ClavePermiso, B.NumeroPermiso, 

                                                C.ClaveCaso, C.IdentificadorCaso, C.ComplementoCaso, 

                                                D.ClaveContribucion, D.FormaPago, D.ImportePago, 

                                                E.TasaContribucion, E.TipoTasa, 

                                                F.Observaciones

                                        FROM `551` A

                                        INNER JOIN `553` B ON B.Fraccion = A.Fraccion

                                                AND (A.SecuenciaFraccion = B.SecuenciaFraccion)

                                                AND (A.auditoria_id = 4 AND B.auditoria_id = 4)

                                                AND (A.Patente = '3452' AND B.Patente = '3452')

                                                AND (A.Pedimento = '0000180' AND B.Pedimento = '0000180') 

                                                AND (A.SeccionAduanera = '430' AND B.SeccionAduanera = '430')

                                        ");

                dd($partidas);

我需要知道我是否可以用雄辩的方式或其他方式发表声明,以便在 Laravel 中获得更好的表现。


Qyouu
浏览 95回答 3
3回答

心有法竹

是的,总有改进的余地。从使用Eloquent Relationship而不是原始 mysql 开始。如果你打算使用 Laravel,那么请仔细阅读文档,没有比这更容易的了。其次,使用select()来尽量减少不必要的数据使用/消耗。优化代码是一项很好的技能。正确使用数据类型。A.Patente = '3452'如果您的 Patente 是int类型,那么与string进行比较将花费更多的处理时间。而是做A.Patente = 3452。将 int 与 int 进行比较。我相信当您按照这些步骤操作时,您的查询不会超过 1 秒。

哈士奇WWW

应该注意的是,我做了删除表的测试,例如我从 A 和 B 开始,它运行良好,ABC 等。直到 F 开始出现缓慢的问题,我想这是因为我有太多的表和记录审查,直到 Pablete 给了我这个选项并且它起作用了。句子如下:$partidas = DB::select("SELECT A.Patente, A.Pedimento, A.SeccionAduanera, A.Fraccion, A.SecuenciaFraccion, A.ValorComercial, A.PrecioUnitario,                                         A.CantidadUMComercial, A.UnidadMedidaComercial, A.CantidadUMTarifa, A.UnidadMedidaTarifa, A.MetodoValorizacion, A.PaisOrigenDestino,                                         A.PaisCompradorVendedor,                                         B.ClavePermiso, B.NumeroPermiso,                                         C.ClaveCaso, C.IdentificadorCaso, C.ComplementoCaso,                                         D.ClaveContribucion, D.FormaPago, D.ImportePago,                                         E.TasaContribucion, E.TipoTasa,                                         F.Observaciones                                 FROM `551` A                                 LEFT JOIN `553` B ON B.Fraccion = A.Fraccion AND (A.SecuenciaFraccion = B.SecuenciaFraccion) AND (A.auditoria_id = B.auditoria_id) AND (A.Patente = B.Patente) AND (A.Pedimento = B.Pedimento) AND (A.SeccionAduanera = B.SeccionAduanera)                                 LEFT JOIN `554` C ON C.Fraccion = B.Fraccion AND (B.SecuenciaFraccion = C.SecuenciaFraccion) AND (B.auditoria_id = C.auditoria_id) AND (B.Patente = C.Patente) AND (B.Pedimento = C.Pedimento) AND (B.SeccionAduanera = C.SeccionAduanera)                                 LEFT JOIN `557` D ON D.Fraccion = A.Fraccion AND (A.SecuenciaFraccion = D.SecuenciaFraccion) AND (A.auditoria_id = D.auditoria_id) AND (A.Patente = D.Patente) AND (A.Pedimento = D.Pedimento) AND (A.SeccionAduanera = D.SeccionAduanera)                                 LEFT JOIN `556` E ON E.Fraccion = A.Fraccion AND (A.SecuenciaFraccion = E.SecuenciaFraccion) AND (A.auditoria_id = E.auditoria_id) AND (A.Patente = E.Patente) AND (A.Pedimento = E.Pedimento) AND (A.SeccionAduanera =  E.SeccionAduanera)                                 LEFT JOIN `558` F ON F.Fraccion = A.Fraccion AND (A.SecuenciaFraccion = F.SecuenciaFraccion) AND (A.auditoria_id = F.auditoria_id) AND (A.Patente = F.Patente) AND (A.Pedimento = F.Pedimento) AND (A.SeccionAduanera = F.SeccionAduanera)                                Where A.auditoria_id = 4                                And A.Patente = '3452'                                And A.Pedimento = '0000180'                                And A.SeccionAduanera = '430'                                ");

手掌心

试试这个它可能有时间像你的 phpmyadmin<?phpuse DB;$pdo = DB::connection()->getPdo();$SQL = "SELECT A.Patente, A.Pedimento, A.SeccionAduanera, A.Fraccion, A.SecuenciaFraccion, A.ValorComercial, A.PrecioUnitario, A.CantidadUMComercial, A.UnidadMedidaComercial, A.CantidadUMTarifa, A.UnidadMedidaTarifa, A.MetodoValorizacion, A.PaisOrigenDestino, A.PaisCompradorVendedor,&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; B.ClavePermiso, B.NumeroPermiso,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; C.ClaveCaso, C.IdentificadorCaso, C.ComplementoCaso,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; D.ClaveContribucion, D.FormaPago, D.ImportePago,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; E.TasaContribucion, E.TipoTasa,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; F.Observaciones&nbsp; &nbsp; FROM `551` A&nbsp; &nbsp; INNER JOIN `553` B ON B.Fraccion = A.Fraccion&nbsp; &nbsp; &nbsp; &nbsp; AND (A.SecuenciaFraccion = B.SecuenciaFraccion)&nbsp; &nbsp; &nbsp; &nbsp; AND (A.auditoria_id = 4 AND B.auditoria_id = 4)&nbsp; &nbsp; &nbsp; &nbsp; AND (A.Patente = '3452' AND B.Patente = '3452')&nbsp; &nbsp; &nbsp; &nbsp; AND (A.Pedimento = '0000180' AND B.Pedimento = '0000180')&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; AND (A.SeccionAduanera = '430' AND B.SeccionAduanera = '430')&nbsp; &nbsp; INNER JOIN `554` C ON C.Fraccion = A.Fraccion&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; AND (A.SecuenciaFraccion = C.SecuenciaFraccion)&nbsp; &nbsp; &nbsp; &nbsp; AND (A.auditoria_id = 4 AND C.auditoria_id = 4)&nbsp; &nbsp; &nbsp; &nbsp; AND (A.Patente = '3452' AND C.Patente = '3452')&nbsp; &nbsp; &nbsp; &nbsp; AND (A.Pedimento = '0000180' AND C.Pedimento = '0000180')&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; AND (A.SeccionAduanera = '430' AND C.SeccionAduanera = '430')&nbsp; &nbsp; INNER JOIN `557` D ON D.Fraccion = A.Fraccion&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; AND (A.SecuenciaFraccion = D.SecuenciaFraccion)&nbsp; &nbsp; &nbsp; &nbsp; AND (A.auditoria_id = 4 AND D.auditoria_id = 4)&nbsp; &nbsp; &nbsp; &nbsp; AND (A.Patente = '3452' AND D.Patente = '3452')&nbsp; &nbsp; &nbsp; &nbsp; AND (A.Pedimento = '0000180' AND D.Pedimento = '0000180')&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; AND (A.SeccionAduanera = '430' AND D.SeccionAduanera = '430')&nbsp; &nbsp; INNER JOIN `556` E ON E.Fraccion = A.Fraccion&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; AND (A.SecuenciaFraccion = E.SecuenciaFraccion)&nbsp; &nbsp; &nbsp; &nbsp; AND (A.auditoria_id = 4 AND E.auditoria_id = 4)&nbsp; &nbsp; &nbsp; &nbsp; AND (A.Patente = '3452' AND E.Patente = '3452')&nbsp; &nbsp; &nbsp; &nbsp; AND (A.Pedimento = '0000180' AND E.Pedimento = '0000180')&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; AND (A.SeccionAduanera = '430' AND E.SeccionAduanera = '430')&nbsp; &nbsp; INNER JOIN `558` F ON F.Fraccion = A.Fraccion&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; AND (A.SecuenciaFraccion = F.SecuenciaFraccion)&nbsp; &nbsp; &nbsp; &nbsp; AND (A.auditoria_id = 4 AND F.auditoria_id = 4)&nbsp; &nbsp; &nbsp; &nbsp; AND (A.Patente = '3452' AND F.Patente = '3452')&nbsp; &nbsp; &nbsp; &nbsp; AND (A.Pedimento = '0000180' AND F.Pedimento = '0000180')&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; AND (A.SeccionAduanera = '430' AND F.SeccionAduanera = '430')";$stmt = $pdo->prepare($SQL);$stmt->execute();var_dump($stmt->fetchAll(PDO::FETCH_ASSOC));
打开App,查看更多内容
随时随地看视频慕课网APP