Oracle OCI 数组获取简单数据类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/101046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 17:13:38  来源:igfitidea点击:

Oracle OCI array fetch of simple data types?

c++cdatabaseoracleoci

提问by HD.

I cannot understand the Oracle documentation. :-(

我无法理解 Oracle 文档。:-(

Does anybody know how to fetch multiple rows of simple data from Oracle via OCI?

有人知道如何通过 OCI 从 Oracle 获取多行简单数据吗?

I currently use OCIDefineByPosto define single variables (I only need to do this for simple integers -- SQLT_INT/4-byte ints) and then fetch a single row at a time with OCIStmtExecute/OCIStmtFetch2.

我目前使用OCIDefineByPos定义单个变量(我只需要为简单整数执行此操作 - SQLT_INT/4 字节整数),然后使用OCIStmtExecute/一次获取一行OCIStmtFetch2

This is OK for small amounts of data but it takes around .5ms per row, so when reading a few ten thousand rows this is too slow.

这对于少量数据是可以的,但每行大约需要 0.5 毫秒,因此在读取几万行时这太慢了。

I just don't understand the documentation for OCIBindArrayOfStruct. How can I fetch a few thousand rows at a time?

我只是不明白OCIBindArrayOfStruct. 如何一次获取几千行?

回答by Zathrus

Have you looked at the sample code in $ORACLE_HOME/oci/samples (if you don't have them installed, then run the Oracle Installer and tell it to install sample code). There are several that use the bulk interfaces.

您是否查看过 $ORACLE_HOME/oci/samples 中的示例代码(如果您没有安装它们,则运行 Oracle 安装程序并告诉它安装示例代码)。有几个使用批量接口。

You may want to seriously consider using a library instead. I've coded Pro*C (hate it), straight OCI, and used 3rd party libraries. The last is the best, by a large margin. The OCI syntax is really hairy and has options you will probably never use. At the same time it is very, very rigid and will crash your code if you do things even slightly wrong.

您可能要认真考虑改用库。我编写了 Pro*C(讨厌它)、直接的 OCI,并使用了 3rd 方库。最后一个是最好的,大幅度提高。OCI 语法真的很麻烦,并且有您可能永远不会使用的选项。同时,它非常非常僵化,如果您做的事情稍微有点错误,它就会使您的代码崩溃。

If you're using C++ then I can recommend OTL; I've done some serious performance testing and OTL is just as fast as hand coding for the general case (you can beat it by 5-10% if you know for certain that you have no NULLs in your data and thus do not need indicator arrays). Note -- do not try to comprehend the OTL code. It's pretty hideous. But it works really well.

如果您使用的是 C++,那么我可以推荐OTL;我已经做了一些严肃的性能测试,并且 OTL 和一般情况下的手工编码一样快(如果你确定你的数据中没有 NULL,那么你可以将它击败 5-10%,因此不需要指标数组)。注意——不要试图理解 OTL 代码。这很可怕。但它真的很好用。

There are also numerous C libraries out there that wrap OCI and make it more usable and less likely to bite you, but I haven't tested any of them.

还有许多 C 库可以包装 OCI 并使其更有用并且不太可能咬你,但我还没有测试过它们中的任何一个。

If nothing else, do yourself a favor and write wrapper functions for the OCI code to make things easier. I did this in my high performance scenario and it drastically reduced the number of issues I had.

如果不出意外,请帮自己一个忙,为 OCI 代码编写包装函数以使事情变得更容易。我在我的高性能场景中这样做了,它大大减少了我遇到的问题数量。

回答by user22943

You can use OCIDefineArrayOfStructto support fetching arrays of records. You do this by passing the base of the array to OCIDefineByPos, and use OCIDefineArrayOfStructto tell Oracle about the size of the records (skip size). I believe that you then call OCIFetchtelling it to fetch the array size.

您可以使用OCIDefineArrayOfStruct来支持获取记录数组。您可以通过将数组的基数传递给OCIDefineByPos, 并用于OCIDefineArrayOfStruct告诉 Oracle 记录的大小(跳过大小)来完成此操作。我相信你然后打电话OCIFetch告诉它获取数组大小。

An alternative is to set the statement attribute, OCI_ATTR_PREFETCH_ROWS, before it is executed. This tells Oracle how many rows to fetch at a time, it defaults to 1. Using this approach, Oracle makes fewer round trips and buffers the rows for you.

另一种方法是OCI_ATTR_PREFETCH_ROWS在执行之前设置语句属性。这告诉 Oracle 一次要获取多少行,默认为 1。使用这种方法,Oracle 减少往返次数并为您缓冲行。

OCIBindArrayOfStructis used with DML statements. It works in a similar fashion to OCIDefineArrayOfStructexcept that it works with bind variables.

OCIBindArrayOfStruct与 DML 语句一起使用。它以类似的方式工作,OCIDefineArrayOfStruct除了它与绑定变量一起工作。

You can find sample code on the Oracle website.

您可以在 Oracle 网站上找到示例代码。