首页 \ 问答 \ 如何构造更复杂的mysql查询以进行优化(How to construct a more complex mysql query for optimization)

如何构造更复杂的mysql查询以进行优化(How to construct a more complex mysql query for optimization)

我有这个可爱的代码片段,我想优化:

$result = mysql_query("select day from cities where name='St Charles'");
$row = mysql_fetch_assoc($result);
$day = $row['day'];

$result = mysql_query("select id,durability from goods_meta_data");

while($row = mysql_fetch_assoc($result)) {
    mysql_query("delete from possessions where day_created<" . ($day-$row[durability]) . " and good_id=" . $row['id']);
}

如果占有已过期,它会从表'所有'中删除行。 占有权是否已过期取决于表格“商品”和“城市”中的值。 具体来说,商品的年龄= day-day_created,如果商品的年龄超过其耐久性,那么它就会过期。 (每一种财产都是一种商品,每种商品都具有独特的耐久性。)

这段代码有效,但我觉得这可以在一个查询中完成。 mysql服务器的延迟特别大,因此在较少的查询中执行此操作将非常有益。

我怎么做? 有任何想法吗?

另外,如果您能指出我可以学习如何以这种方式更好地利用关系数据库的任何有用资源,那将会很有帮助。


I have this lovely piece of code that I want to optimize:

$result = mysql_query("select day from cities where name='St Charles'");
$row = mysql_fetch_assoc($result);
$day = $row['day'];

$result = mysql_query("select id,durability from goods_meta_data");

while($row = mysql_fetch_assoc($result)) {
    mysql_query("delete from possessions where day_created<" . ($day-$row[durability]) . " and good_id=" . $row['id']);
}

It deletes rows from the table 'possessions' if the possession has expired. Whether or not the possession has expired is determined by values in the tables 'goods' and 'cities'. Specifically, the age of the good = day-day_created, if the age of the good is more than its durability, then it is expired. (Every possession is a type of good, and every good has a unique durability.)

This code works, but I feel like this could be done in a single query. The latency to the mysql server is particularly large, so doing this operation in less queries would be very beneficial.

How do I do that? Any ideas?

Also, if you can point me to any useful resources where I can learn about taking better advantage of relational databases in this manner, it would be helpful.


原文:https://stackoverflow.com/questions/17644838
更新时间:2024-04-25 11:04

最满意答案

请注意,这一切都取决于实现。 C标准甚至没有说出堆栈,堆等字样。 它只讨论了变量所期望的行为,这取决于它们的存储( staticexternregister等)。

如此说来,通常arg将位于为功能提供的堆栈框架中。 它的范围仅限于some_int范围内的some_int

顺便说一句,它不在堆上,它有一个静态的全局存储。


Note that everything of this is implementation dependent. The C standard does not even utter the words stack, heap and so on. It just talks about the behavior that is expected from variables depending on their storage(static,extern,register etc).

Having said so usually arg will be located in the stack frame which is provided for the function. It's scope is limited to the function just as scope of some_int.

By the way sth is not on heap it has a static global storage.

相关问答

更多
  • 如果您查看$MyInvocation的文档,您会看到.... $MyInvocation仅填充脚本,函数和脚本块。 您可以使用$ MyInvocation在当前脚本中返回的System.Management.Automation.InvocationInfo对象中的信息,例如脚本的路径和文件名( $MyInvocation.MyCommand.Path ) 或函数的名称 ( $MyInvocation.MyCommand.Name )来标识当前命令。 您可以看到函数内的$MyInvocation.MyCom ...
  • 如果你的字符串“可能包含空字符”,那么它不是一个真正的C字符串。 如果您需要从一个内存位置复制一定数量的字节到另一个位置,则无论内容如何,都可以使用memcpy 。 你可以使用sizeof来获得字符串文字的sizeof (这个大小包括最终的,隐式的空终止符)。 If you string "may contain the null character," then it isn't really a C string. If you need to copy a certain number of byte ...
  • 1.未定义的行为不是崩溃 首先请记住,当你用内存做坏事(比如在变量被破坏后处理它)时,结果是未定义的行为 ,这意味着与“崩溃”完全不同。 未定义的行为意味着任何事情都可能发生(包括崩溃),但任何事情都可能意味着“没有”。 实际上,最糟糕的类型的错误是那些未定义的行为不会立即显示任何事情的错误,但只会在代码的其他一些不相关且无关紧要的部分中引发疯狂的行为。 或者只有在广大受众面前展示您的节目时。 所以请记住,未定义的行为不会崩溃。 只有幸运时才会崩溃。 你越早理解bug和崩溃之间的区别就越好。 虫子是你的敌人 ...
  • ReadFile一个指向缓冲区的指针,它可以将数据写入其中。 您正在传递NULL,因此您会看到错误。 我会将代码更改为 // ReadFile function variables static const DWORD bytesToRead = 100; unsigned char dataRead[bytesToRead]; DWORD bytesWritten = 0; //------------------------------------------- ...
  • 请注意,这一切都取决于实现。 C标准甚至没有说出堆栈,堆等字样。 它只讨论了变量所期望的行为,这取决于它们的存储( static , extern , register等)。 如此说来,通常arg将位于为功能提供的堆栈框架中。 它的范围仅限于some_int范围内的some_int 。 顺便说一句,它不在堆上,它有一个静态的全局存储。 Note that everything of this is implementation dependent. The C standard does not even ...
  • 假设顶层函数是ajax回调函数,您可以将变量保存在全局范围的变量中,以便从第二个函数中访问该变量。 即: 全球范围: var ajaxData; 第一功能: function(data){ logger.info(data.Description,data.Title,clickNotiCustomer) // assign global variable to data ajaxData = data; } 第二个功能: $http.post('/feeds/clearOneNoti', ...
  • 一个问题可能是您正在修改Read函数中的局部变量T : int *T; Read(T, len); // ... void Read(int T[], int len) { int i; T = (int*) malloc(len * sizeof *T); // ... } Read内部的T是真实T变量的副本。 您为副本分配新值,但原始值保持不变。 要实际修改外部T ,请将指针传递给它,如下所示: int *T; Read(&T, len); // ... void Read ...
  • char * replaceURLS(char ** body) { char *newString = (char *) malloc(strlen(*body) + 1); strcpy(newstring, *body); *body = newString; return(newString + whateverOffsetWithinTheStringYouNeedToReturn); } char * replaceURLS(char ** body) { ...
  • 你可以尝试一下。 基本上,它是进入对象中任何级别并更新属性的通用方法。 代码注释中的详细信息 var User = { local: { location: { city: "", state: "" } } } var Profile = { location: { city: "", state: "" } } function update(obj, path, updatedProps) { ...
  • 如果您使用的是Visual C ++,则查找数据断点 。 If you are using Visual C++ then look up data breakpoints.

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)