0

我不确定我是否在这里做错了什么或者是什么问题

但是当我运行这个查询时

EXPLAIN SELECT * 
FROM reservation AS rs
INNER JOIN vehicles AS ve ON rs.vehicle_id = ve.vehicles_id
INNER JOIN customers AS cu ON rs.customerid = cu.customerid
LEFT JOIN checks AS ck ON rs.reservation_id = ck.reservation_id
LEFT JOIN credits AS cr ON rs.reservation_id = cr.reservation_id

我明白了

id  select_type table   type    possible_keys   key key_len ref rows    Extra
1   SIMPLE  rs  ALL NULL    NULL    NULL    NULL    22   
1   SIMPLE  ve  eq_ref  PRIMARY PRIMARY 4   crm.rs.Vehicle_id   1    
1   SIMPLE  ck  ALL NULL    NULL    NULL    NULL    2    
1   SIMPLE  cr  ALL NULL    NULL    NULL    NULL    5    
1   SIMPLE  cu  eq_ref  PRIMARY PRIMARY 4   crm.rs.CustomerID   1   Using where

这里的问题是我在联接中使用的所有字段都是索引或主键。有所有初级

ve.vehicles_id
cu.customerid
ck.reservation_id
cr.reservation_id

and these are all indexes
rs.vehicle_id
rs.customerid

这是信息检查

CREATE TABLE `checks` (
  `Checks_id` int(11) unsigned NOT NULL auto_increment,
  `customerID` int(11) unsigned NOT NULL,
  `firstActivity` datetime NOT NULL,
  `lastActivity` datetime NOT NULL,
  `checkValue` varchar(7) NOT NULL,
  `dueDate` datetime NOT NULL,
  `Reservation_id` int(11) unsigned NOT NULL,
  `date` datetime NOT NULL,
  `isCashed` int(11) NOT NULL default '0',
  `cashingResult` int(2) NOT NULL default '0',
  PRIMARY KEY  (`Checks_id`),
  KEY `customerID` (`customerID`),
  KEY `Reservation_id` (`Reservation_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8


            CREATE TABLE `vehicles` (
              `Vehicles_id` int(11) unsigned NOT NULL auto_increment,
              `name` varchar(50) NOT NULL,
              `size` varchar(20) NOT NULL,
              `maker` varchar(20) NOT NULL,
              `model` varchar(40) NOT NULL,
              `year` year(4) NOT NULL,
              `color` varchar(20) NOT NULL,
              `oilChange` varchar(10) NOT NULL,
              `currentMillage` varchar(10) NOT NULL,
              `oilChangeMillage` varchar(10) NOT NULL,
              `registrationExp` datetime NOT NULL,
              `insuranceExp` datetime NOT NULL,
              `dailyRate` double NOT NULL default '0',
              `weekleyRate` double NOT NULL default '0',
              `monthleyRate` double NOT NULL default '0',
              `addDate` datetime NOT NULL,
              `Active` int(1) NOT NULL default '1',
              `VINnum` varchar(17) NOT NULL,
              `licensePlate` varchar(10) NOT NULL,
              `vehicleCost` double NOT NULL,
              PRIMARY KEY  (`Vehicles_id`),
              UNIQUE KEY `VINnum` (`VINnum`)
            ) ENGINE=MyISAM AUTO_INCREMENT=11 DEFAULT CHARSET=utf8


        CREATE TABLE `credits` (
          `Credits_id` int(11) unsigned NOT NULL auto_increment,
          `customerID` int(11) unsigned NOT NULL,
          `creditValue` varchar(6) NOT NULL,
          `dueDate` datetime NOT NULL,
          `Reservation_id` int(11) unsigned NOT NULL,
          `date` datetime NOT NULL,
          `firstActivity` datetime NOT NULL,
          `lastActivity` datetime NOT NULL,
          `paymentHistory` text NOT NULL,
          `paymentHistoryDate` text NOT NULL,
          PRIMARY KEY  (`Credits_id`),
          KEY `customerID` (`customerID`),
          KEY `Reservation_id` (`Reservation_id`)
        ) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8

    CREATE TABLE `reservation` (
      `Reservation_id` int(11) unsigned NOT NULL auto_increment,
      `Vehicle_id` int(11) unsigned NOT NULL,
      `CustomerID` int(11) unsigned NOT NULL,
      `additionalDrivers` char(54) NOT NULL,
      `dateFrom` datetime NOT NULL,
      `dateTo` datetime NOT NULL,
      `pickupDate` datetime NOT NULL,
      `dropoffDate` datetime NOT NULL,
      `addDate` datetime NOT NULL,
      `reservationStatus` tinyint(1) NOT NULL default '1' COMMENT '0 canceled, 1 Reserved, 2 In Progress, 3 Completed',
      `totalDays` int(4) NOT NULL,
      `totalDiscount` char(6) NOT NULL,
      `totalFees` char(6) NOT NULL,
      `totalRent` double NOT NULL,
      `totalTax` double NOT NULL,
      `totalChecks` double NOT NULL,
      `totalOwe` double NOT NULL default '0',
      `totalPaidCash` double NOT NULL default '0',
      `totalPaidBankCard` double NOT NULL default '0',
      PRIMARY KEY  (`Reservation_id`),
      KEY `CustomerID` (`CustomerID`),
      KEY `Vehicle_id` (`Vehicle_id`)
    ) ENGINE=MyISAM AUTO_INCREMENT=1033 DEFAULT CHARSET=utf8

所以我不确定为什么我仍然为可能键和键得到 NULL ?

我究竟做错了什么?我所有的主键都具有相同的值 INT(11) unsiged。

谢谢你们的帮助

4

1 回答 1

0

由于您没有用于过滤的 WHERE 条件,因此列出的第一个表将包含所有行,因此没有可用的索引是有意义的。在您的情况下,扫描所有reservation表格。

至于checkscredits表,您将必须显示来自 的表结构输出SHOW CREATE TABLE tablename \G。从您显示的查询中我看不出为什么会这样。我猜测索引列之间可能存在列类型不匹配,这会阻止 mysql 使用它们来比较行。

于 2013-02-26T04:32:44.367 回答