为什么解释查询在主键的连接上返回NULL

时间:2013-02-26 04:18:47

标签: php mysql

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

但是当我运行此查询时

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

所以我不确定为什么我仍然会为possible_key和key获取NULL?

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

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

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

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