简体   繁体   中英

Mysql Php, queries are very slow

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

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

  • 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM