(底部有更新)
我正在尝试获取表中名为"VersionHistory“的最新条目,由于id设置为自动增量,所以我试图获取最大ID。尽量避免按降序对整个表进行排序,并尽可能减少表增长时对此查询所需的计算,而且这个表可能会变得非常快。
class VersionHistoryQuery extends BaseVersionHistoryQuery {
public function getLatestVersion() {
return $this
->withColumn('MAX(version_history.id)')
->limit(1);
}
}我在VersionHistory构造函数中调用函数,如下所示:
class VersionHistory extends BaseVersionHistory {
public function __construct($backupPath = "") {
$lastVersion = VersionHistoryQuery::create()
->getLatestVersion()
->find();
$lastVersion->setBackupPath("backup/" . $backupPath);
$lastVersion->save();
parent::setImportedAt(date("Y-m-d H:i:s"));
}
}这将在php中输出一个“允许内存大小耗尽”错误。知道为什么吗?注释掉VersionHistory构造函数中的查询将修复错误,因此它位于查询的某个位置。我尝试按照下面的说明设置一个自定义查询:http://propelorm.org/documentation/03-basic-crud.html#using-custom-sql。但我不能让它起作用。跑步:
SELECT * FROM version_history WHERE id = (SELECT MAX(id) FROM version_history)从MySQL工作台可以很好地、快速地工作。
知道我做错了什么吗?
我试过的
更新守则如下:
public function getLatestVersion() {
return $this
->orderById('desc')
->findOne();
}仍然得到相同的内存分配错误。
更新守则如下:
$lastVersion = VersionHistoryQuery::create()
->orderById('desc')
->findOne();删除自定义函数,打开propel调试模式,它将输出此查询的运行情况:
[2015-10-11 17:26:54] shop_reporting_db.INFO: SELECT `version_history`.`id`, `version_history`.`imported_at`, `version_history`.`backup_path` FROM `version_history` ORDER BY `version_history`.`id` DESC LIMIT 1 [] []仍然会遇到内存溢出。
发布于 2015-10-11 20:32:31
仅此而已:
SELECT * FROM version_history ORDER BY id DESC LIMIT 1;发布于 2015-10-11 20:39:24
从文档中,withColumn执行以下操作:
Propel将'with‘列添加到查询的SELECT子句中,并使用withColumn()调用的第二个参数作为列别名。
因此,看起来您实际上是在查询表中的每一行,而且每行都在查询最大ID。
我对propel一无所知(除了我刚刚搜索到的内容),但是看起来您需要一种不同的方式来指定您的where条件。
发布于 2017-08-11 19:19:52
原始SQL和推进查询是不同的/不等价的。
在propel查询中,只添加了一列,而原始SQL实际上有两个查询,其中一个是子查询,另一个是子查询。
因此,您需要在推进方面做相应的工作:
$lastVersionID = VersionHistoryQuery::create()
->withColumn('MAX(id)', 'LastVersionID')
->select('LastVersionID')
->findOne();
$lastVersion = VersionHistoryQuery::create()
->filterByVersionHistory($lastVersionID)
->find();注意->select('LatestVersionID'),因为您只需要一个标量值,而不需要整个对象,以及使用withColumn()的虚拟列(别名在withColumn()中)。
https://stackoverflow.com/questions/33069790
复制相似问题