TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Why your query language should be explicit

44 点作者 hiphipjorge将近 10 年前

21 条评论

cwyers将近 10 年前
&gt; In this query, we get all the users with the name &#x27;jorge&#x27; are queried and then ordered in descending order by age.<p>&gt; SELECT * FROM users WHERE name = &#x27;jorge&#x27; ORDER BY age;<p>&gt; If we wanted to dig deeper into this query, we might want to know if the &quot;WHERE&quot; is getting executed before the &quot;ORDER BY&quot;. Can we tell from the query if this is the case? No, we can&#x27;t. You&#x27;d have to look it up.<p>No I wouldn&#x27;t. I can look right at that query and tell you what order those two things occur in. The order makes sense -- of course you don&#x27;t sort the results before you filter the results, the SQL database is not a moron. In fact, in this weird &quot;explicit&quot; query language, you have to remember the SAME ORDER -- put the WHERE in front of the ORDER BY (or the filter in front of the orderBy, in RethinkQL logic). Except if you forget the order, you can end up creating a query that performs many times worse than it has to. Whereas in SQL, even if you forget the most basic information about the query plan possible, the query planner will choose a pretty good execution strategy for your query.<p>And if you can&#x27;t figure out how that simple SQL query is going to be executed by the server just by reading it, why on Earth do you need a query language that does not just allow but requires you to to set your own query plan? The ability to shoot yourself in the foot isn&#x27;t a feature.
评论 #9742362 未加载
评论 #9742743 未加载
评论 #9742491 未加载
评论 #9742768 未加载
geophile将近 10 年前
I don&#x27;t agree with this conclusion. I&#x27;ve worked with Postgres, MySQL and Oracle, and found that it is important to have a good understanding of the possible execution plans for a query. I will sometimes construct a query very carefully to achieve a particular plan. And when things go wrong, I run EXPLAIN PLAN, examine statistics, etc., and tweak my query to do what I want. You really have to do that to obtain good performance in some cases. It does undercut the claim that SQL has to being non-procedural, but that&#x27;s life.<p>I do NOT want to have to construct the query execution plan manually for every single query. Usually, the optimizer will do a fine job, assuming the database designer has made good choices regarding indexes and other physical design issues. But there are always a few complex, tricky queries where you do have to understand internals. The &quot;cognitive load&quot; is the same, and having a high-level query language means that when you do make your subtle and deft change to rescue performance, you often just tweak a query, instead of rewriting a detailed query plan.<p>(Extending this position a little: I have come around 180 degrees and consider ORMs to be a bad idea overall. When you do need to be very careful about writing a query, the ORM adds a layer of complexity -- not only do you need to control the SQL, but you now need to ensure that your ORM can produce that SQL.)
评论 #9742186 未加载
评论 #9742062 未加载
评论 #9741925 未加载
Xophmeister将近 10 年前
SQL is declarative: you tell the RDBMS what you want and its job is to do it. That&#x27;s how SQL is and how it&#x27;s always been. A better RDBMS will optimise the execution plan for you. The point about indexing is somewhat valid, but that&#x27;s an integral part of schema design and something one should define from the outset based on the data&#x27;s intended use.<p>By &#x27;explicit&#x27;, what I presume the author to mean is &#x27;transparent&#x27;. I agree that development processes should be transparent, but I don&#x27;t necessarily agree that imperative is better than declarative. Indeed, the use case for SQL is data manipulation and analysis; arguably that doesn&#x27;t come under the remit of &#x27;development&#x27;, even though programming is involved. Hence the prerequisite of a properly setup schema by someone who knows what they&#x27;re doing!<p>Declarative languages <i>definitely</i> have their place.
评论 #9742431 未加载
batbomb将近 10 年前
The biggest problem with a procedural&#x2F;explicit query is a dynamic system. Without a query planner, you don&#x27;t have the luxury of a system rewriting your queries. When Table A ~ Table B, but then Table B &gt;&gt; Table A, your queries are going to be radically suboptimal.<p>Of course, if you&#x27;re never joining, maybe that&#x27;s not such a big problem, but you&#x27;d have the same issue of a specific range in your table grows disproportionally to another range in the same table and the index you are using is incorrect.<p>With SQL you can often be very explicit using Common Table Expressions as well in DBs that support it. Otherwise, using subqueries, GROUP BY with HAVING, and several other features often, but not always, prevent radically rewritten queries.<p>Finally, with SQL on many DBMSs you can still get the explicit wiggle room you need using optimizer hints. No, it&#x27;s not very portable, but neither is ReQL.<p>Edit: A few examples of being explicit in SQL (a lá Oracle):<p><pre><code> SELECT &#x2F;*+ INDEX(name) *&#x2F; * FROM users WHERE users.name = &#x27;jorge&#x27;; WITH users_by_name AS ( SELECT &#x2F;*+ INDEX(name) *&#x2F; * FROM users WHERE users.name = &#x27;jorge&#x27; ) SELECT * FROM users_by_name JOIN profile using (user_id) ORDER BY age;</code></pre>
评论 #9741902 未加载
评论 #9742227 未加载
rwallace将近 10 年前
He says he&#x27;s been using the explicit query language for a couple of months. On that timescale, I can see how it might still feel okay. But as the months become years and your code grows in complexity to meet an ever lengthening requirements list, it should become apparent why SQL is far superior.<p>Need to add an index to speed up some queries?<p>In SQL, you add the index and you&#x27;re done.<p>In an explicit query language, you add the index and... oops, that doesn&#x27;t do anything. You&#x27;ve got to go back and inspect every single query, anywhere in your program, that could potentially benefit from that index, to see whether it actually will, and if so, modify it by hand.<p>Switching from SQL to an explicit query language converts certain types of programming effort from O(N) to O(N^2). This is one of the reasons SQL was invented in the first place.
kragen将近 10 年前
Jorge writes:<p>&gt; Does this increase cognitive load? Yes, it does. But this is outweighed by the ability to understand how your query is being executed. … Hence, when you see a query you immediately know that it&#x27;s using an index…<p>This reminds me of something I read in a paper once:<p>&gt; Accordingly, it provides a basis for a high level data language which will yield maximal independence between programs on the one hand and machine representation and organization of data on the other.<p>It seems like this Jorge dude is claiming that it&#x27;s great that, if you use his company&#x27;s product, you have to change your program when you change the representation and organization of data on your disk, and that there are no real disadvantages to this. I think maybe he should read the paper I&#x27;m quoting from above, which is Codd 1970, introducing the relational database: <a href="https:&#x2F;&#x2F;www.seas.upenn.edu&#x2F;~zives&#x2F;03f&#x2F;cis550&#x2F;codd.pdf" rel="nofollow">https:&#x2F;&#x2F;www.seas.upenn.edu&#x2F;~zives&#x2F;03f&#x2F;cis550&#x2F;codd.pdf</a> — Codd explains why the 1960s CODASYL systems similar to RethinkDB made programs unmaintainable.<p>If you don’t understand why relational databases got adopted in the first place, you aren’t qualified to “rethink databases” or to call yourself a “full-stack developer”. And your gullible customers, although they may get a prototype built quickly, will be outcompeted by their rivals who aren’t afraid of using query optimizers. Jorge must think we&#x27;re all fucking idiots who don&#x27;t know why we abandoned products like RethinkDB thirty or forty years ago.<p>Fortunately, the HN thread is much more intelligent and informed than the original article!
jasode将近 10 年前
I don&#x27;t agree with the essay&#x27;s premise.<p><i>&gt;Having your query language be explicit means that you&#x27;ve hit at exactly the right level of abstraction: not too much, but not too little. </i><p>I don&#x27;t like the label of &quot;explicit&quot; as if it&#x27;s an objective indicator on the continuum between low and high abstraction. It comes across as a value judgement that we&#x27;d all agree on and I don&#x27;t think there&#x27;s obvious consensus on architecting a data access language.<p><i>&gt;If we wanted to dig deeper into this query, we might want to know if the &quot;WHERE&quot; is getting executed before the &quot;ORDER BY&quot;. Can we tell from the query if this is the case? No, we can&#x27;t. You&#x27;d have to look it up.</i><p>Not knowing the internals of execution is actually a <i>deliberate design feature</i> of SQL. The SQL is meant to be a <i>declarative</i> statement that expresses an algebraic set of rows. (But sometimes, the mathematical purity of this abstraction &quot;leaks&quot; and DBAs&#x2F;Devs have to add HINTS or do SQL EXPLAIN PLAN to dig into what&#x27;s happening under the hood -- but that&#x27;s a separate issue.)<p>I suppose if one really wanted to affect the order of operations at the SQL syntax level, one could write a VIEW or a subquery with the ORDER BY and then write the outer query with the WHERE clause. I haven&#x27;t tested this to see if any of the major SQL engines would rewrite this type of convoluted SQL of ORDER BY -then- WHERE clause.<p>Yes, with UNIX command line, you have different execution characteristics of &quot;ls | grep | sort&quot; vs &quot;ls | sort | grep&quot; but one can&#x27;t translate that explicit-sequence-of-execution mental model to SQL.<p>* Does this increase cognitive load? Yes, it does. But this is outweighed by the ability to understand how your query is being executed.<p>I&#x27;m not convinced of this conclusion.<p>Also, I&#x27;m not sure RethinkDB works like this as a deliberately engineered advantage. The RethinkDB devs can clarify but it&#x27;s possible for their engine to work like this because it&#x27;s more straightforward to implement the parser and not because there is overwhelming inherent superiority to this approach. With traditional SQL (e.g. Oracle, MSSQL, etc), the query rewriting engines are very mature and can be more aggressive and thereby fulfilling the goal of a declarative mathematical purity. However, it takes lots of programmer man-hours to translate declarative SQL into optimal execution plans.
jmileham将近 10 年前
This is a great way to spin not having a query planner as a feature, but I&#x27;m glad to have one every day that I write and compose semantic bits of SQL that can and should have different execution plans depending on the context in which they&#x27;re evaluated.
评论 #9742114 未加载
mnarayan01将近 10 年前
As others have already mentioned, what the author is calling &quot;explicit&quot; would be more typically called &quot;imperative&quot;. I&#x27;m going to go further though and say that I think &quot;explicit&quot; as used here is actually <i>wrong</i>. Take the example from the article:<p><pre><code> SELECT * FROM users WHERE name = &#x27;jorge&#x27; ORDER BY age; </code></pre> and assume that we have exactly one index on the table, namely a compound one on `name, age`. If that index is used, then _only_ the filtering need be done. In SQL, because the execution sequence is left <i>unspecified</i>, we can continue to write the query as is while still allowing the DB to skip the unneeded ordering step (whether it does so or not is a different question obviously).<p>If, however, the execution sequence must be &quot;specified&quot; (cf. &quot;explicit&quot;) and you don&#x27;t want to perform the unnecessary ordering step, then either the order must be left out of the query (and thus <i>implicit</i>), or the DB needs to be able to ignore what you tell it to do.
meritt将近 10 年前
No. Learn to trust the query planner. It&#x27;s a good thing. When you want explicitness, take a look at the explain plan.
takeda将近 10 年前
I think this is a silly argument. It&#x27;s much easier to write a database without query planner than with a query planner.<p>The reason why you would want to have implicit language is because an optimal query might be different depending on what data you have and even what are you querying.<p>For example if table has only 5 jorges it&#x27;s probably better to use an index, but if majority of users are jorges or the table is very small it&#x27;s far more efficient to just scan it.
orf将近 10 年前
I hate to criticize but this stood out to me:<p>&gt; SELECT * FROM users WHERE name = &#x27;jorge&#x27; ORDER BY age;<p>&gt; Can we tell from the query if this is the case? No, we can&#x27;t. You&#x27;d have to look it up.<p>and then:<p>&gt; r.table(&#x27;users&#x27;).filter({ name: &#x27;jorge&#x27; }).orderBy(r.desc(&#x27;age&#x27;))<p>&gt; Now, can you tell from the query if the users are filtered or ordered first? Yes! filter comes first.<p>The filter comes first in both queries. It&#x27;s exactly the same.<p>The part about indexes is interesting though.
评论 #9742583 未加载
TheLoneWolfling将近 10 年前
What I want is a language with <i>feedback</i>.<p>In other words, a language that calculates all of the various optimizations behind-the-scenes, and sees what ones it thinks would be good and suggests them to you. And you can add annotations to allow it to do specific optimizations.<p>It has control and transparency, but keeps it relatively easy to optimize. And you can hide the annotations if you really wish.
ksherlock将近 10 年前
This seems like a massive step backwards. And it&#x27;s not for the benefit of the user (the programmer, she needs to do more work). It&#x27;s for the benefit of the RethinkDB programmer (query planners are hard work!). Add an index in SQL? Existing queries work and can make use of it. Add an index in RethinkDB? Now go rewrite all your code if you want to take advantage of it. That&#x27;s an improvement? (And do you think the average javascript programmer will do a better job at it than 30 years of database research?)<p>If you&#x27;re trying to spin shit into gold, maybe you should try a rethink preprocessor. Just write normal SQL in your code and the pre-processor verifies the tables and columns, checks for indices, and writes the best &quot;explicit&quot; query for you.
kylepdavis将近 10 年前
I think explicit languages can make things more clear however disagree with the notion that implicit behaviors are necessarily a bad thing.<p>I&#x27;ve found that the hybrid approach in the MongoDB aggregation framework works really well.<p>It optimizes things around the first $match to create an optimized initial read (the selectivity of your initial stages is really important). Once you&#x27;re past the initial read the rest of the pipeline is fully imperative.<p>This makes things really nice when debugging complex aggregation pipelines. For example, you can simply omit the rest of your pipeline at any point to debug (with a $limit), see what you&#x27;re dealing with, fix them, and move on to the next one.
jtwebman将近 10 年前
Have you built any really big data projects with RethinkDB or just demo apps? There are some big advantages to having query planners and optimizers in the database. Also in SQL you can be explicit. You can tell it what index to use.
oconnor663将近 10 年前
&gt; <i>But RethinkDB won&#x27;t optimize the query for you or tell you it&#x27;s wrong. It&#x27;ll just run it. It&#x27;s up to the developer to understand what&#x27;s going on and optimize accordingly. This might sound like a huge deal, but the simplicity of the language makes it easy to spot these inefficiencies and fix them accordingly.</i><p>That&#x27;s the kind of thing that&#x27;s only true until it&#x27;s not. The bigger a query gets, the less likely that you&#x27;ll be able to eyeball it to see what you screwed up.
ris将近 10 年前
Besides the fact that I don&#x27;t agree with his conclusion - he ignores any of the advantages of an implicit language and therefore the article suffers from the &quot;everybody must be stupid&quot; syndrome...<p>&quot;Jorge Silva, Dev Evangelist @ RethinkDB. Full-Stack JavaScript Developer.&quot;<p>That final sentence makes me cringe slightly.
评论 #9742187 未加载
rusabd将近 10 年前
5 years later there will be an article about great new feature in XX-DB - a query planner.
jamesrom将近 10 年前
A lot of people are saying &#x27;just trust the query planner&#x27;.<p>The best of both worlds would be the ability to explicitly define how to get what you want just as easily as you can define what you want.<p>That should be the goal.
评论 #9742809 未加载
DrScump将近 10 年前
&quot;In this query, we get (sic) all the users with the name &#x27;jorge&#x27; are queried and then ordered in descending order by age.&quot;<p>Am I missing something, or should that say &quot;ASCending&quot; ?
评论 #9742805 未加载