我什么时候应该使用 C++而不是 SQL?

发布于 2024-11-18 11:55:48 字数 384 浏览 5 评论 0原文

我是一名 C++ 程序员,偶尔使用 MySQL 来处理数据库,但我的 SQL 知识相当有限。但我确实愿意改变这一点。

目前,我正在尝试仅使用 SQL 查询对数据库中的数据进行分析(!)。但我准备放弃了,转而将数据导入到C++中,用C++代码进行分析。

我和同事讨论过这个问题,他们也推动我使用C++,说SQL不是为了复杂的分析而主要是导入(从现有表)和导出(到新表)数据,还有一点例如将数据合并到连接表中。

有人可以帮我画一条线吗?那么我知道什么时候该切换到 C++ 吗?当然性能也是一个问题。

有哪些迹象表明 SQL 中的情况变得复杂?或者也许我只是在设计查询时采用了错误的方法。那么我在哪里可以找到教程、书籍……以采取更好的方法?

我希望这不是太含糊。我真的有点失落了。

I am a C++ programmer who occasionally uses MySQL to work with databases, but my SQL knowledge is rather limited. However I am surely willing to change that.

At the moment I am trying to do analysis(!) on the data I have in my database solely with SQL queries. But I am about to give up, and instead import the data to C++ and do the analysis with C++ code.

I have discussed this with my colleagues, and they also push me to use C++, saying that SQL is not meant for complex analysis but mainly for importing (from the existing tables) and exporting (to new tables) data, and a little bit more such as merging data to - e.g. - joined tables.

Can somebody help me drawing a line? So I know when to switch to C++? Of course performance is also an issue.

What are indications that things get to complex in SQL? Or maybe I just take the wrong approach with designing the queries. Then where can I find tutorials, books, ... to take a better approach?

I hope this is not too vague. I am really a bit lost.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

鹿港小镇 2024-11-25 11:55:48

SQL 擅长分析大量关系数据

划清界限的地方是分析的规模。

如果您每次分析单个记录,请在您的应用程序中进行。

如果您将大量记录作为一个单元进行分析,那么 SQL 绝对是完成该工作的最佳工具。

SQL 的设计或优化并不是针对逐行分析进行很好的设计或优化。但是,如果您想了解有关一百万行数据组的信息,请在数据库中进行。

SQL excels at analyzing large sets of relational data.

The place to draw the line is the scale of your analysis.

If you analyze individual records one at a time, do it in your application.

If you analyze large sets of records as a unit, SQL is definitely the best tool for that job.

Row-by-row analysis is not something SQL is designed or optimized for very well. But, if you want to know something about a million-row group of data, do it in the database.

稳稳的幸福 2024-11-25 11:55:48

我和同事讨论过这个问题,他们也推动我使用C++,说SQL不是为了复杂的分析而主要是导入(从现有表)和导出(到新表)数据,并且更多一点,例如将数据合并到 - 例如 - 连接表。

这完全是任意的。学习 SQL。网络上有很多免费资源。

I have discussed this with my colleagues, and they also push me to use C++, saying that SQL is not meant for complex analysis but mainly for importing (from the existent tables) and exporting (to new tables) data, and a little bit more such as merging data to - e.g. - joined tables.

This is completely arbitrary. Learn SQL. There are a lot of resources available on the web for free.

荆棘i 2024-11-25 11:55:48

如果您知道如何使用 SQL 提供的功能,则可以使用 SQL 对数据进行非常复杂的分析。

SQL 具有执行关系操作的功能,例如连接和投影。也可用于执行集合运算,如并集、交集和限制(子集)。还可以用于对数字进行基本算术运算,例如四个算术运算符,以及 SQRT 等内置函数。还有 COUNT、SUM 和 AVG 等统计函数,它们可以以非常有趣的方式与投影结合起来。一个好的 DBMS 可以让您使用用 C、C++ 或 PL/SQL 编写的自己的函数来扩展内置函数。

您从这些功能中获得的能力取决于数据库的设计程度。设计良好的数据库符合关系模型,并且应该与您对数据的预期用途相关。

SQL代码可以存储在数据库中的存储过程中。它可以存储在 SQL 脚本文件中。而且,正如您所知,它可以嵌入到应用程序中。除了 SQL 之外,您还可以使用 OLAP 工具和报告生成器轻松地对数据执行标准操作。

那些建议你用 C++ 进行所有处理的人听起来好像他们已经学会了足够的知识来使用像一个大而愚蠢的文件系统这样的数据库。一个好的 DBMS 远不止于此。

You can do very complex analysis of data in SQL, provided you know how use the features that SQL offers.

SQL has features for doing relational operations, like joins and projections. Also for doing set operations like union, intersection, and restriction (subset). Also for doing basic arithmetic on numbers, like the four arithmetic operators, and built in functions like SQRT. Also statistical functions like COUNT, SUM, and AVG that can be combined with projections in very interesting ways. A good DBMS will let you extend the built in functions with your own functions written in C, C++ or maybe PL/SQL.

The power you get from these features depends on how well designed the database is. A well designed database conforms to the relational model, and should be relvant to your intended use of the data.

SQL code can be stored in the database in stored prodecures. It can be stored in SQL script files. And, as you already know, it can be embedded in application programs. In addition to SQL, you can use OLAP tools and report generators to do standard things with the data very easily.

The people who advise you to keep all of your processing in C++ sound like they have learned just enough to use a database like a big and stupid file system. A good DBMS is much more than that.

似梦非梦 2024-11-25 11:55:48

SQL 通常可以非常有效地处理其自己的数据库(取决于服务器实现)。

您应该使用查询来分析数据库。
主要原因是通信开销。
即使服务器位于本地计算机上(远程服务器会有明显的通信开销),您仍然需要将存储的信息从 SQL 服务器检索到 C++ 程序以进行分析。

现在,如果 SQL 中有 10000 行,则必须让 SQL 服务器读取所有这些行并将它们发送到您的程序,在程序中它可能会创建数据的本地副本供您处理。

如果您让 SQL Server 通过查询来完成此操作,您将根据您正在执行的查询类型获得复杂的优化,最终您只能检索有限数量的数据(您实际需要的数据)通过沟通。

SQL is usually very efficient handling its own database (depends on the server implementation).

You should use queries to analyze the database.
The main reason for that would be the communication overhead.
Even if the server is on the local machine (remote servers would have obvious communication overhead), you'll still have to retrieve the stored information from the SQL server to your c++ program for analysis.

Now if you have 10000s of lines in the SQL you would have to get the SQL server to read them all and send them to your program where it would probably create a local copy of the data for you to work on.

If you let the SQL server do it with queries, you'll gain the complex optimizations it does according the kind of query you're executing, and in the end you can retrieve only a limited amount of data (the one you actually need) through the communication.

奶茶白久 2024-11-25 11:55:48

您做出了正确的决定,开始使用 SQL 进行数据分析。现在,当您觉得您的 SQL 知识限制了您时,您有 2 个选择:放弃并切换回熟悉但不是很高效的工具集 (C++),或者提高您的 SQL 水平。

有可能在某个时候 SQL 也会变得太复杂,但那时 C++ 也不会是答案 - 很可能是一些专门的工具。

You made right decision to begin data analysis with SQL. Now, when you feel that your knowledge of SQL limits you, you have 2 choices: give up and switch back to familiar but not very efficient toolset (C++) or bring your level with SQL up.

It's possible that at some point SQL will become too complex too, but then C++ won't be the answer either - most likely some specialized tools.

谁对谁错谁最难过 2024-11-25 11:55:48

在我看来,如果数据库服务器没有提供等效的分析功能,您应该只在 C++ 中执行分析,因为数据库服务器非常智能,很难且几乎不可能击败数据库服务器分析功能的算法效率。此外,将原始数据引入应用程序进行分析还需要大量开销。

如果在某些时候普通 SQL 变得过于复杂,服务器的本机 PL 可能是一个不错的选择

In my opinion you should only perform analysis in C++ if no equivalent for the analysis function is provided by database server, As database servers are very smart and it is hard and almost imposible to beat the algorithm efficiency of analysis function of database server. Also bringing raw data to the application for performing analysis also includes lots of overheads.

If at some point plain SQL becomes overly complex native PL of the sever could be a good choice

何必那么矫情 2024-11-25 11:55:48

我同意 JNK 和 Jochai 的观点,但不同意 Ascanio 的观点。
最好提高数据库系统方面的知识。
自带sql

I agree with JNK and Jochai, but disagree with Ascanio.
It's better to improve the knowledge in database systems.
Sql comes with it

没︽人懂的悲伤 2024-11-25 11:55:48

所以,这是我一直在思考的事情,在我看来,SQL 作为一种用于存储/操作数据的平台/语言,与 C++ 或 C 库相比不应该具有固有的优势。在我看来,理论上你可以构建一个 C++ 库,与 SQL 执行此操作一样高效,甚至更高效。这样做,您将能够从头开始构建它,根据整数、字符、字符串和其他数据类型的存储方式,并使其更容易与特定应用程序(如 Web 开发)进行交互。您甚至可以使用像 javascript 这样的语言来完成查询(允许 Web 开发人员专注于很好地学习一种语言)。

So, this is something I've been thinking about and it seems to me that SQL, as just a platform/language for storing/manipulating data, should have no inherent advantage over a C++ or C library. It seems to me that theoretically you could build a C++ library just as efficient, if not more efficient, than SQL at doing this. In doing so, you would be able to build it from the ground up, in terms of how ints, chars, strings, and other data types are stored, and make it easier to interface with you particular application (like web development). You could even make it so that the queries could be done in a language like javascript (allowing web developers to focus on just learning one language really well).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文