{"id":203,"date":"2026-07-12T09:46:56","date_gmt":"2026-07-12T01:46:56","guid":{"rendered":"https:\/\/yykcj.com\/?p=203"},"modified":"2026-07-12T09:46:56","modified_gmt":"2026-07-12T01:46:56","slug":"orm-vs-raw-sql%ef%bc%9a%e9%80%89%e5%9e%8b%e4%b8%8e%e5%b9%b3%e8%a1%a1%e7%ad%96%e7%95%a5%ef%bc%8c%e5%ae%9e%e6%88%98%e5%af%b9%e6%af%94%e4%b8%8e%e6%b7%b7%e5%90%88%e4%bd%bf%e7%94%a8%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/yykcj.com\/?p=203","title":{"rendered":"ORM vs RAW SQL\uff1a\u9009\u578b\u4e0e\u5e73\u8861\u7b56\u7565\uff0c\u5b9e\u6218\u5bf9\u6bd4\u4e0e\u6df7\u5408\u4f7f\u7528\u6307\u5357"},"content":{"rendered":"<h2>\u80cc\u666f<\/h2>\n<p>\u5728Web\u5f00\u53d1\u4e2d\uff0c\u6570\u636e\u5e93\u8bbf\u95ee\u5c42\u9009\u578b\u662f\u6bcf\u4e2a\u56e2\u961f\u90fd\u4f1a\u9762\u4e34\u7684\u7ecf\u5178\u95ee\u9898\u3002ORM\uff08\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\uff09\u5982Prisma\u3001TypeORM\u3001Django ORM\u7b49\uff0c\u627f\u8bfa\u63d0\u5347\u5f00\u53d1\u6548\u7387\u3001\u51cf\u5c11\u6837\u677f\u4ee3\u7801\uff1b\u800c\u539f\u751fSQL\u5219\u63d0\u4f9b\u6781\u81f4\u6027\u80fd\u4e0e\u7075\u6d3b\u6027\u3002\u7136\u800c\uff0c\u8bb8\u591a\u56e2\u961f\u5728\u9009\u578b\u65f6\u9677\u5165\u975e\u6b64\u5373\u5f7c\u7684\u8bef\u533a\uff0c\u5bfc\u81f4\u540e\u671f\u7ef4\u62a4\u56f0\u96be\u6216\u6027\u80fd\u74f6\u9888\u3002\u672c\u6587\u5c06\u57fa\u4e8e\u771f\u5b9e\u9879\u76ee\u7ecf\u9a8c\uff0c\u5206\u6790\u4e24\u8005\u7684\u672c\u8d28\u5dee\u5f02\uff0c\u63d0\u4f9b\u4e00\u5957\u53ef\u843d\u5730\u7684\u9009\u578b\u7b56\u7565\uff0c\u5e76\u6f14\u793a\u5982\u4f55\u5728\u540c\u4e00\u4e2a\u9879\u76ee\u4e2d\u4f18\u96c5\u5730\u6df7\u5408\u4f7f\u7528ORM\u548c\u539f\u751fSQL\u3002<\/p>\n<h2>\u6838\u5fc3\u5bf9\u6bd4\uff1aORM vs RAW SQL<\/h2>\n<h3>1. \u5f00\u53d1\u6548\u7387<\/h3>\n<ul>\n<li><strong>ORM<\/strong>\uff1a\u81ea\u52a8\u751f\u6210CRUD\u4ee3\u7801\uff0c\u8fc1\u79fb\u7ba1\u7406\u65b9\u4fbf\uff0c\u7c7b\u578b\u5b89\u5168\uff08\u5982Prisma\uff09\u3002\u9002\u5408\u5feb\u901f\u539f\u578b\u3001\u6807\u51c6\u64cd\u4f5c\u3002<\/li>\n<li><strong>RAW SQL<\/strong>\uff1a\u9700\u624b\u5199\u6240\u6709\u67e5\u8be2\uff0c\u8c03\u8bd5\u7e41\u7410\uff0c\u4f46\u65e0\u5b66\u4e60\u6210\u672c\uff08SQL\u662f\u901a\u7528\u8bed\u8a00\uff09\u3002<\/li>\n<\/ul>\n<h3>2. \u6027\u80fd<\/h3>\n<ul>\n<li><strong>ORM<\/strong>\uff1a\u5e38\u4ea7\u751fN+1\u67e5\u8be2\u95ee\u9898\uff0c\u590d\u6742JOIN\u6548\u7387\u4f4e\uff0c\u5185\u5b58\u5360\u7528\u9ad8\u3002<\/li>\n<li><strong>RAW SQL<\/strong>\uff1a\u53ef\u7cbe\u7ec6\u63a7\u5236\u6267\u884c\u8ba1\u5212\uff0c\u5229\u7528\u6570\u636e\u5e93\u7279\u6027\uff08\u7a97\u53e3\u51fd\u6570\u3001CTE\u7b49\uff09\u3002<\/li>\n<\/ul>\n<h3>3. \u7ef4\u62a4\u6027<\/h3>\n<ul>\n<li><strong>ORM<\/strong>\uff1a\u6a21\u578b\u53d8\u66f4\u81ea\u52a8\u8fc1\u79fb\uff0c\u4ee3\u7801\u53ef\u8bfb\u6027\u9ad8\u3002\u4f46\u590d\u6742\u4e1a\u52a1\u903b\u8f91\u9690\u85cf\u5728ORM\u65b9\u6cd5\u4e2d\uff0c\u8c03\u8bd5\u56f0\u96be\u3002<\/li>\n<li><strong>RAW SQL<\/strong>\uff1a\u6240\u6709\u903b\u8f91\u663e\u5f0f\u53ef\u89c1\uff0c\u4f46SQL\u6563\u843d\u5728\u4ee3\u7801\u4e2d\uff0c\u96be\u4ee5\u91cd\u6784\u3002<\/li>\n<\/ul>\n<h3>4. \u5b89\u5168\u6027<\/h3>\n<ul>\n<li><strong>ORM<\/strong>\uff1a\u5185\u7f6e\u53c2\u6570\u5316\u67e5\u8be2\uff0c\u9632SQL\u6ce8\u5165\u3002<\/li>\n<li><strong>RAW SQL<\/strong>\uff1a\u9700\u624b\u52a8\u5904\u7406\u53c2\u6570\u7ed1\u5b9a\uff0c\u5bb9\u6613\u758f\u5ffd\u3002<\/li>\n<\/ul>\n<h2>\u9009\u578b\u51b3\u7b56\u6811<\/h2>\n<p>\u57fa\u4e8e\u9879\u76ee\u9636\u6bb5\u548c\u573a\u666f\uff0c\u6211\u603b\u7ed3\u4e86\u4e00\u4e2a\u5b9e\u7528\u51b3\u7b56\u6811\uff1a<\/p>\n<ol>\n<li><strong>\u539f\u578b\/\u5feb\u901f\u8fed\u4ee3\u9636\u6bb5<\/strong> \u2192 \u4f18\u5148ORM\uff0c\u51cf\u5c11\u5fc3\u667a\u8d1f\u62c5\u3002<\/li>\n<li><strong>\u6807\u51c6CRUD\u64cd\u4f5c<\/strong> \u2192 \u4f7f\u7528ORM\u3002<\/li>\n<li><strong>\u590d\u6742\u67e5\u8be2\uff08\u591a\u8868JOIN\u3001\u805a\u5408\u3001\u5b50\u67e5\u8be2\uff09<\/strong> \u2192 \u539f\u751fSQL\u3002<\/li>\n<li><strong>\u6027\u80fd\u654f\u611f\u8def\u5f84\uff08\u9ad8\u5e76\u53d1\u3001\u5927\u6570\u636e\u91cf\uff09<\/strong> \u2192 \u539f\u751fSQL + \u7f13\u5b58\u3002<\/li>\n<li><strong>\u62a5\u8868\/\u5206\u6790\u7c7b\u67e5\u8be2<\/strong> \u2192 \u539f\u751fSQL\uff08\u6216\u4f7f\u7528\u6570\u636e\u5e93\u89c6\u56fe\uff09\u3002<\/li>\n<li><strong>\u56e2\u961f\u6280\u80fd<\/strong>\uff1a\u82e5\u56e2\u961fSQL\u80fd\u529b\u5f31\uff0c\u5219\u504f\u91cdORM\uff1b\u53cd\u4e4b\u53ef\u66f4\u591a\u4f7f\u7528\u539f\u751fSQL\u3002<\/li>\n<\/ol>\n<p><strong>\u6838\u5fc3\u539f\u5219<\/strong>\uff1a80%\u7684\u7b80\u5355\u64cd\u4f5c\u4f7f\u7528ORM\uff0c20%\u7684\u590d\u6742\/\u6027\u80fd\u5173\u952e\u64cd\u4f5c\u4f7f\u7528\u539f\u751fSQL\u3002<\/p>\n<h2>\u5b9e\u6218\uff1a\u5728Node.js\u9879\u76ee\u4e2d\u6df7\u5408\u4f7f\u7528Prisma\u548c\u539f\u751fSQL<\/h2>\n<p>\u6211\u4eec\u4ee5Prisma\uff08ORM\uff09\u548cPostgreSQL\u4e3a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u6df7\u5408\u4f7f\u7528\u3002<\/p>\n<h3>\u9879\u76ee\u521d\u59cb\u5316<\/h3>\n<pre><code class=\"language-bash\">\nnpm init -y\nnpm install @prisma\/client prisma\nnpx prisma init\n<\/code><\/pre>\n<h3>\u5b9a\u4e49\u6a21\u578b\uff08schema.prisma\uff09<\/h3>\n<pre><code class=\"language-prisma\">\ndatasource db {\n  provider = &quot;postgresql&quot;\n  url      = env(&quot;DATABASE_URL&quot;)\n}\n\ngenerator client {\n  provider = &quot;prisma-client-js&quot;\n}\n\nmodel User {\n  id        Int      @id @default(autoincrement())\n  name      String\n  email     String   @unique\n  posts     Post[]\n  createdAt DateTime @default(now())\n}\n\nmodel Post {\n  id        Int      @id @default(autoincrement())\n  title     String\n  content   String?\n  published Boolean  @default(false)\n  author    User     @relation(fields: [authorId], references: [id])\n  authorId  Int\n  createdAt DateTime @default(now())\n}\n<\/code><\/pre>\n<h3>\u6267\u884c\u8fc1\u79fb<\/h3>\n<pre><code class=\"language-bash\">\nnpx prisma migrate dev --name init\n<\/code><\/pre>\n<h3>\u6807\u51c6CRUD\u4f7f\u7528Prisma<\/h3>\n<pre><code class=\"language-typescript\">\nimport { PrismaClient } from &#x27;@prisma\/client&#x27;;\n\nconst prisma = new PrismaClient();\n\nasync function createUser(name: string, email: string) {\n  return prisma.user.create({\n    data: { name, email },\n  });\n}\n\nasync function getUserPosts(userId: number) {\n  return prisma.user.findUnique({\n    where: { id: userId },\n    include: { posts: true },\n  });\n}\n<\/code><\/pre>\n<h3>\u590d\u6742\u67e5\u8be2\u4f7f\u7528\u539f\u751fSQL<\/h3>\n<p>\u5f53\u9700\u8981\u7edf\u8ba1\u6bcf\u4e2a\u7528\u6237\u7684\u6587\u7ae0\u6570\u5e76\u6392\u5e8f\u65f6\uff0cORM\u53ef\u80fd\u4ea7\u751fN+1\u95ee\u9898\u6216\u4f4e\u6548\u67e5\u8be2\u3002\u6b64\u65f6\u4f7f\u7528\u539f\u751fSQL\uff1a<\/p>\n<pre><code class=\"language-typescript\">\nimport { PrismaClient } from &#x27;@prisma\/client&#x27;;\n\nconst prisma = new PrismaClient();\n\nasync function getUserPostCounts() {\n  const result = await prisma.$queryRaw`\n    SELECT u.id, u.name, COUNT(p.id) as &quot;postCount&quot;\n    FROM &quot;User&quot; u\n    LEFT JOIN &quot;Post&quot; p ON p.&quot;authorId&quot; = u.id\n    GROUP BY u.id, u.name\n    ORDER BY &quot;postCount&quot; DESC\n  `;\n  return result;\n}\n<\/code><\/pre>\n<p>> <strong>\u6ce8\u610f<\/strong>\uff1a<code>$queryRaw<\/code> \u8fd4\u56de\u7684\u662f\u65e0\u7c7b\u578b\u6570\u636e\uff0c\u5efa\u8bae\u4f7f\u7528 <code>Prisma.sql<\/code> \u6a21\u677f\u6807\u7b7e\u5e76\u5b9a\u4e49\u8fd4\u56de\u7c7b\u578b\u3002<\/p>\n<h3>\u6027\u80fd\u5173\u952e\u8def\u5f84\u4f7f\u7528\u539f\u751fSQL<\/h3>\n<p>\u4f8b\u5982\uff0c\u9700\u8981\u6279\u91cf\u66f4\u65b0\u6587\u7ae0\u72b6\u6001\uff0cORM\u4f1a\u9010\u6761\u6267\u884c\uff0c\u800c\u539f\u751fSQL\u53ef\u4ee5\u4e00\u6b21\u5b8c\u6210\uff1a<\/p>\n<pre><code class=\"language-typescript\">\nasync function publishPostsByUser(userId: number) {\n  await prisma.$executeRaw`\n    UPDATE &quot;Post&quot;\n    SET published = true\n    WHERE &quot;authorId&quot; = ${userId} AND published = false\n  `;\n}\n<\/code><\/pre>\n<h3>\u5c01\u88c5\uff1a\u521b\u5efaRepository\u5c42<\/h3>\n<p>\u4e3a\u4fdd\u6301\u4ee3\u7801\u6574\u6d01\uff0c\u5efa\u8bae\u5c06\u6570\u636e\u5e93\u64cd\u4f5c\u5c01\u88c5\u5728Repository\u4e2d\uff0c\u5bf9\u5916\u66b4\u9732\u7edf\u4e00\u63a5\u53e3\uff0c\u5185\u90e8\u51b3\u5b9a\u4f7f\u7528ORM\u6216SQL\u3002<\/p>\n<pre><code class=\"language-typescript\">\n\/\/ user.repository.ts\nimport { PrismaClient } from &#x27;@prisma\/client&#x27;;\n\nconst prisma = new PrismaClient();\n\nexport class UserRepository {\n  \/\/ \u7b80\u5355\u64cd\u4f5c\u4f7f\u7528ORM\n  async findById(id: number) {\n    return prisma.user.findUnique({ where: { id } });\n  }\n\n  \/\/ \u590d\u6742\u64cd\u4f5c\u4f7f\u7528\u539f\u751fSQL\n  async getTopUsersByPostCount(limit: number) {\n    return prisma.$queryRaw`\n      SELECT u.id, u.name, COUNT(p.id) as &quot;postCount&quot;\n      FROM &quot;User&quot; u\n      LEFT JOIN &quot;Post&quot; p ON p.&quot;authorId&quot; = u.id\n      GROUP BY u.id, u.name\n      ORDER BY &quot;postCount&quot; DESC\n      LIMIT ${limit}\n    `;\n  }\n}\n<\/code><\/pre>\n<h2>\u5e38\u89c1\u5751\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h2>\n<h3>1. \u907f\u514dN+1\u67e5\u8be2<\/h3>\n<p>ORM\u4e2d\u82e5\u672a\u6b63\u786e\u4f7f\u7528<code>include<\/code>\u6216<code>select<\/code>\uff0c\u4f1a\u5bfc\u81f4\u5faa\u73af\u67e5\u8be2\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-typescript\">\n\/\/ \u274c \u9519\u8bef\uff1a\u5faa\u73af\u4e2d\u67e5\u8be2\u6570\u636e\u5e93\nconst users = await prisma.user.findMany();\nfor (const user of users) {\n  const posts = await prisma.post.findMany({ where: { authorId: user.id } });\n}\n\n\/\/ \u2705 \u6b63\u786e\uff1a\u4f7f\u7528include\u4e00\u6b21\u67e5\u8be2\nconst usersWithPosts = await prisma.user.findMany({\n  include: { posts: true },\n});\n<\/code><\/pre>\n<h3>2. \u53c2\u6570\u5316\u67e5\u8be2\u9632\u6ce8\u5165<\/h3>\n<p>\u4f7f\u7528<code>$queryRaw<\/code>\u65f6\uff0c\u52a1\u5fc5\u4f7f\u7528\u6a21\u677f\u63d2\u503c\uff08<code>${}<\/code>\uff09\uff0c\u4e0d\u8981\u62fc\u63a5\u5b57\u7b26\u4e32\u3002<\/p>\n<pre><code class=\"language-typescript\">\n\/\/ \u274c \u5371\u9669\uff1aSQL\u6ce8\u5165\u98ce\u9669\nawait prisma.$queryRaw`SELECT * FROM &quot;User&quot; WHERE name = &#x27;${userInput}&#x27;`;\n\n\/\/ \u2705 \u5b89\u5168\uff1a\u53c2\u6570\u5316\nawait prisma.$queryRaw`SELECT * FROM &quot;User&quot; WHERE name = ${userInput}`;\n<\/code><\/pre>\n<h3>3. \u4e8b\u52a1\u7ba1\u7406<\/h3>\n<p>\u6df7\u5408\u4f7f\u7528\u65f6\uff0c\u4e8b\u52a1\u9700\u7edf\u4e00\u7ba1\u7406\u3002Prisma\u652f\u6301\u5d4c\u5957\u4e8b\u52a1\uff1a<\/p>\n<pre><code class=\"language-typescript\">\nawait prisma.$transaction([\n  prisma.user.create({ data: { name: &#x27;Alice&#x27;, email: &#x27;alice@test.com&#x27; } }),\n  prisma.$executeRaw`UPDATE &quot;Post&quot; SET published = true WHERE &quot;authorId&quot; = 1`,\n]);\n<\/code><\/pre>\n<h3>4. \u7c7b\u578b\u5b89\u5168<\/h3>\n<p>\u539f\u751fSQL\u8fd4\u56de\u7ed3\u679c\u65e0\u7c7b\u578b\uff0c\u5efa\u8bae\u624b\u52a8\u5b9a\u4e49\u63a5\u53e3\u6216\u4f7f\u7528<code>zod<\/code>\u9a8c\u8bc1\u3002<\/p>\n<pre><code class=\"language-typescript\">\ninterface TopUser {\n  id: number;\n  name: string;\n  postCount: bigint;\n}\n\nconst result = await prisma.$queryRaw&lt;TopUser[]&gt;`\n  SELECT u.id, u.name, COUNT(p.id) as &quot;postCount&quot;\n  FROM &quot;User&quot; u\n  LEFT JOIN &quot;Post&quot; p ON p.&quot;authorId&quot; = u.id\n  GROUP BY u.id, u.name\n`;\n<\/code><\/pre>\n<h2>\u603b\u7ed3<\/h2>\n<ul>\n<li><strong>ORM<\/strong> \u9002\u5408\u6807\u51c6CRUD\u548c\u5feb\u901f\u5f00\u53d1\uff0c<strong>\u539f\u751fSQL<\/strong> \u9002\u5408\u590d\u6742\u67e5\u8be2\u548c\u6027\u80fd\u4f18\u5316\u3002<\/li>\n<li>\u91c7\u7528\u201c80\/20\u6cd5\u5219\u201d\uff1a80%\u64cd\u4f5c\u4f7f\u7528ORM\uff0c20%\u4f7f\u7528\u539f\u751fSQL\u3002<\/li>\n<li>\u901a\u8fc7Repository\u5c42\u9694\u79bb\u5b9e\u73b0\u7ec6\u8282\uff0c\u4fbf\u4e8e\u7ef4\u62a4\u548c\u6d4b\u8bd5\u3002<\/li>\n<li>\u59cb\u7ec8\u6ce8\u610f\u5b89\u5168\u6027\uff08\u53c2\u6570\u5316\u67e5\u8be2\uff09\u548c\u6027\u80fd\uff08\u907f\u514dN+1\uff09\u3002<\/li>\n<\/ul>\n<p><strong>\u4e0b\u4e00\u6b65<\/strong>\uff1a\u5c1d\u8bd5\u5728\u4f60\u7684\u9879\u76ee\u4e2d\u5f15\u5165\u6df7\u5408\u7b56\u7565\uff0c\u4ece\u6700\u590d\u6742\u7684\u67e5\u8be2\u5f00\u59cb\u66ff\u6362\u4e3a\u539f\u751fSQL\uff0c\u89c2\u5bdf\u6027\u80fd\u63d0\u5347\u3002<\/p>\n<h2>\u5ef6\u4f38\u9605\u8bfb<\/h2>\n<ul>\n<li><a href=\"https:\/\/www.prisma.io\/docs\/concepts\/components\/prisma-client\/raw-database-access\">Prisma\u5b98\u65b9\u6587\u6863\uff1a\u539f\u751f\u67e5\u8be2<\/a><\/li>\n<li><a href=\"https:\/\/typeorm.io\/#\/raw-sql\">TypeORM\u4e2d\u4f7f\u7528\u539f\u751fSQL<\/a><\/li>\n<li><a href=\"https:\/\/use-the-index-luke.com\/\">\u6570\u636e\u5e93\u6027\u80fd\u4f18\u5316\uff1a\u7d22\u5f15\u4e0e\u67e5\u8be2\u8ba1\u5212<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u6df1\u5165\u5bf9\u6bd4ORM\u4e0e\u539f\u751fSQL\u7684\u4f18\u7f3a\u70b9\uff0c\u63d0\u4f9b\u57fa\u4e8e\u9879\u76ee\u9636\u6bb5\u7684\u9009\u578b\u51b3\u7b56\u6811\uff0c\u5e76\u5c55\u793a\u5982\u4f55\u5728\u540c\u4e00\u4e2a\u9879\u76ee\u4e2d\u6df7\u5408\u4f7f\u7528\u4e24\u8005\uff0c\u517c\u987e\u5f00\u53d1\u6548\u7387\u4e0e\u6027\u80fd\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[5,121,123,122,6,124],"class_list":["post-203","post","type-post","status-publish","format-standard","hentry","category-3","category-4","tag-node-js","tag-orm","tag-prisma","tag-sql","tag-6","tag-124"],"_links":{"self":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/203","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=203"}],"version-history":[{"count":0,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/203\/revisions"}],"wp:attachment":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}