English 中文(简体)
Mysql Php,查询非常慢
原标题:Mysql Php, queries are very slow

我有一个托管在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
问题回答

我发现使用SQL_CALC_found_ROWS的速度非常慢。。而且,在没有限制的情况下只获取和复制查询几乎比使用mysqlnumrows生成现有行数更快。

如果这有帮助,请告诉我

  • 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




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签