Mysql Php,查询很慢

发布于 2024-10-17 09:07:30 字数 2576 浏览 2 评论 0原文

我有一个托管在 sandbox.promls.net 上的应用程序,

我用于检索服务器信息的查询存在一些问题,我仍处于开发阶段,因此没有太多数据加载到数据库中。

这是我正在执行的查询(它是一个视图):

select SQL_CALC_FOUND_ROWS id , name, contact, email_contact, phone_contact, address, phone, fax, email, website, creation_date, last_modification, zipcode, longitude, latitude, gmtoffset, dstoffset, area_id, area, status , logo, type, owner_id, users, created_by, created_by_id 
    from companies_listing 
    limit 0,15

执行需要 19.6522991657 秒。请帮助我!

视图的结构如下:

视图的结构如下:

 DROP VIEW IF EXISTS `companies_listing`;
CREATE OR REPLACE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` 
  SQL SECURITY DEFINER VIEW `companies_listing` AS
  select `c`.`id` AS `id`, `c`.`name` AS `name`,`c`.`contact` AS `contact`,
    `c`.`phone_contact` AS `phone_contact`,`c`.`email_contact` AS `email_contact`,
    `c`.`address` AS `address`,`c`.`phone` AS `phone`,`c`.`fax` AS `fax`,
    `c`.`owner_id` AS `owner_id`,`c`.`email` AS `email`,
    `c`.`website` AS `website`,`c`.`creation_date` AS `creation_date`,
    `c`.`last_modification` AS `last_modification`,`c`.`zipcode` AS `zipcode`,
    `c`.`type` AS `type`,`c`.`status` AS `status`,`a`.`description` AS `area`,
    `c`.`area_id` AS `area_id`,`c`.`logo` AS `logo`,
    `c`.`created_by` AS `creator_id`,`u`.`fullname` AS `creator`,
    (select count(0) AS `count(*)` from `users` `uu` 
       where (`uu`.`company_id` = `c`.`id`)
    ) AS `users` 
    from (
       (`company` `c` 
       join `areas` `a` 
        on((`a`.`id` = `c`.`area_id`))
       )
       join `users` `u` on((`u`.`id` = `c`.`created_by`))
    );

Query explain select id , name, contact, email_contact, phone_contact, address,
                phone, fax, email, website, creation_date, last_modification, 
                area_id, area, status , logo, type, owner_id, users, creator,
                creator_id
                from companies_listing, Thu Feb 10 17:45:37 2011

id   select_type             table       type     possible_keys  key    key_len ref rows    Extra
1   PRIMARY                 <derived2>  ALL       10             (null)
2   DERIVED                 c           ALL       FK_company_1_company              18  (null)
2   DERIVED                 u           eq_ref      PRIMARY PRIMARY 4   inmobili.c.created_by   1   (null)
2   DERIVED                 a           eq_ref      PRIMARY PRIMARY 4   inmobili.c.area_id  1   (null)
3   DEPENDENT SUBQUERY      uu          ref fk_user_company fk_user_company 4   inmobili.c.id   1   Using index

I have an application hosted on sandbox.promls.net

There are some issues with the queries that I'm using for retrieve information to my server I'm still on development phase so there is not much data loaded into the database.

This is the query I'm executing (it's a view):

select SQL_CALC_FOUND_ROWS id , name, contact, email_contact, phone_contact, address, phone, fax, email, website, creation_date, last_modification, zipcode, longitude, latitude, gmtoffset, dstoffset, area_id, area, status , logo, type, owner_id, users, created_by, created_by_id 
    from companies_listing 
    limit 0,15

It takes 19.6522991657 seconds to execute. Help me please!

the structure of view is the following:

the view structure is the following:

 DROP VIEW IF EXISTS `companies_listing`;
CREATE OR REPLACE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` 
  SQL SECURITY DEFINER VIEW `companies_listing` AS
  select `c`.`id` AS `id`, `c`.`name` AS `name`,`c`.`contact` AS `contact`,
    `c`.`phone_contact` AS `phone_contact`,`c`.`email_contact` AS `email_contact`,
    `c`.`address` AS `address`,`c`.`phone` AS `phone`,`c`.`fax` AS `fax`,
    `c`.`owner_id` AS `owner_id`,`c`.`email` AS `email`,
    `c`.`website` AS `website`,`c`.`creation_date` AS `creation_date`,
    `c`.`last_modification` AS `last_modification`,`c`.`zipcode` AS `zipcode`,
    `c`.`type` AS `type`,`c`.`status` AS `status`,`a`.`description` AS `area`,
    `c`.`area_id` AS `area_id`,`c`.`logo` AS `logo`,
    `c`.`created_by` AS `creator_id`,`u`.`fullname` AS `creator`,
    (select count(0) AS `count(*)` from `users` `uu` 
       where (`uu`.`company_id` = `c`.`id`)
    ) AS `users` 
    from (
       (`company` `c` 
       join `areas` `a` 
        on((`a`.`id` = `c`.`area_id`))
       )
       join `users` `u` on((`u`.`id` = `c`.`created_by`))
    );

Query explain select id , name, contact, email_contact, phone_contact, address,
                phone, fax, email, website, creation_date, last_modification, 
                area_id, area, status , logo, type, owner_id, users, creator,
                creator_id
                from companies_listing, Thu Feb 10 17:45:37 2011

id   select_type             table       type     possible_keys  key    key_len ref rows    Extra
1   PRIMARY                 <derived2>  ALL       10             (null)
2   DERIVED                 c           ALL       FK_company_1_company              18  (null)
2   DERIVED                 u           eq_ref      PRIMARY PRIMARY 4   inmobili.c.created_by   1   (null)
2   DERIVED                 a           eq_ref      PRIMARY PRIMARY 4   inmobili.c.area_id  1   (null)
3   DEPENDENT SUBQUERY      uu          ref fk_user_company fk_user_company 4   inmobili.c.id   1   Using index

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

慕烟庭风 2024-10-24 09:07:30

我发现使用 SQL_CALC_FOUND_ROWS 非常慢..并且在没有限制的情况下获取和复制查询几乎比使用 mysql_num_rows 生成存在的行数要快。

让我知道这是否有帮助

I've found that using SQL_CALC_FOUND_ROWS is very slow all and all.. and that it's almost faster to just take and replicate the query without the limit than use mysql_num_rows to generate the amount of rows that exists.

Let me know if this helps

一口甜 2024-10-24 09:07:30
  • FROM 子句中的括号不会增加易读性,而且会影响效率(因为主查询在不必要时会变成“派生”),
  • 您应该在所有列上建立索引您正在加入/查询的 (`c`.`area_id``c`.`created_by` 是显而易见的
  • Those parentheses in your FROM clause aren't adding legibility, and they have impact on efficiency (as the main query becomes "derived" when it doesn't have to)
  • you should make indexes on all the colums that you are joining/querying by (`c`.`area_id` and `c`.`created_by` are the obvious ones
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文