MySQL选择查询太慢\

时间:2016-02-02 12:50:58

标签: mysql

我有一个包含90个数据字段的表和1500个记录。当我执行Select查询并尝试拉取整个表记录时,它变得太慢。即使我将其限制为100条记录,也需要超过30秒。

任何建议都将不胜感激。

查询如下:

SELECT 
  ID, FirstName, LastName, 
  date_format( InitialConsultDate , '%m/%d/%Y' ) AS InitialConsultDate, 
  Address1, Address2, City, State, Zip, HomeNumber, WorkNumber, CellNumber, Email, 
  Height, Weight, Age, Sex, Married, BirthDate, UnderPhysicianCare, MedicalIssue, 
  Smoking, Exercise, NumberTimesPerWeek, Dieting, AdequateWater, Cellulite, 
  BiggestConcern, CustomerDeal, LipoSessionsPurchased, FacialSessionsPurchased, 
  NutritionSessionsPurchased, ReferredBy, AdditionalNotes, WeightEvaluationDate1, 
  Weight1, WeightEvaluationDate2, Weight2, WeightEvaluationDate3, Weight3, BMI, 
  ClientBeforePictureFront, ClientAfterPictureFront1, ClientAfterPictureFront2, 
  ClientBeforePictureLeftSide, ClientAfterPictureLeftSide1, ClientAfterPictureLeftSide2, 
  ClientBeforePictureRightSide, ClientAfterPictureRightSide1, 
  ClientAfterPictureRightSide2, ClientMeasurementH1, ClientMeasurementH2, 
  ClientMeasurementH3, Area1Before, Area1After, Area2Before, Area2After, 
  Area3Before, Area3After, IsOldCustomer, ClientAfterFront3, ClientAfterFront4, 
  ClientAfterFront5, ClientAfterLeftSide3, ClientAfterLeftSide4, 
  ClientAfterLeftSide5, ClientAfterRightSide3, ClientAfterRightSide4, 
  ClientAfterRightSide5, ClientBeforeBack, ClientAfterBack1, ClientAfterBack2,
  ClientAfterBack3, ClientAfterBack4, ClientAfterBack5, Area1After1, Area1After2, 
  Area1After3, Area1After4, Area1After5, Area2After1, Area2After2, Area2After3, 
  Area2After4, Area2After5, Area3After1, Area3After2, Area3After3, Area3After4, 
  Area3After5 
FROM customer 
WHERE IsOldCustomer ='0' 
ORDER BY ID

2 个答案:

答案 0 :(得分:0)

您应该使用索引来优化数据搜索,例如:

CREATE INDEX idx_name ON your_table (field_name);
CREATE UNIQUE INDEX idx_name ON your_table (field_name);
  • 将第一个用于具有重复值的字段(例如:date, 名称等。)
  • 将第二个用于具有唯一值的字段 (例如:登录,身份证等......)

答案 1 :(得分:0)

为了让您了解情况,请遵循以下建议:

  1. 包含90多列的表格并不表示良好的数据库设计[尽管可能存在例外情况]。使您的数据库更加规范化。
  2. WHERE子句中的列上使用索引(按照其他用户的建议)。阅读所有关于Indexes
  3. 的信息
  4. 虽然现在可能没有问题,但如果您的数据量可能会增长,您可能需要考虑为您的表格创建PARTITION
  5. 现在,当然,在IsOldCustomer列上创建索引可能就是这样做的。