a.mat_list_status IN (1,3,4)用 EXISTS 代替in
时间: 2024-03-30 09:38:38 浏览: 22
可以使用EXISTS代替IN的写法,如下所示:
SELECT *
FROM TABLE_A a
WHERE EXISTS (
SELECT 1
FROM TABLE_B b
WHERE b.mat_list_status IN (1, 3, 4)
AND a.mat_list_status = b.mat_list_status
);
这里使用EXISTS子句将TABLE_B表连接到主查询中,从而达到替代IN的效果。在子查询中,我们筛选出mat_list_status为1、3、4的记录,然后将其与TABLE_A表中的记录进行比较,如果存在相同的mat_list_status,就返回TABLE_A表中的记录。
相关问题
select a.job_id, a.job_name, a.job_group, a.invoke_target, a.cron_expression, a.misfire_policy, a.concurrent, a.status, a.create_by, a.create_time, a.remark, a.job_des, a.cron_des, b.status as last_execute_status, b.create_time as last_execute_time from sys_job a left join sys_job_log b ON a.job_name = b.job_name and a.job_group = b.job_group where b.create_time = (select max(create_time) from sys_job_log c where a.job_name = c.job_name and a.job_group = c.job_group) or b.create_time is null有重复数据,运行环境为oracle11g,请对其进行优化
可以考虑对该SQL语句进行以下优化:
1. 使用子查询代替LEFT JOIN:将子查询的结果作为一个临时表,再和sys_job表进行关联,可以减少关联表的数量,提高查询效率。
2. 使用EXISTS代替子查询:EXISTS用于查询是否存在符合条件的记录,比子查询更高效,可以进一步提高查询效率。
3. 添加索引:对sys_job_log表的job_name、job_group、create_time字段添加联合索引,可以加速子查询和关联查询。
优化后的SQL语句如下:
```
SELECT a.job_id, a.job_name, a.job_group, a.invoke_target, a.cron_expression, a.misfire_policy, a.concurrent, a.status, a.create_by, a.create_time, a.remark, a.job_des, a.cron_des, b.status AS last_execute_status, b.create_time AS last_execute_time
FROM sys_job a
LEFT JOIN sys_job_log b ON a.job_name = b.job_name AND a.job_group = b.job_group AND b.create_time = (SELECT MAX(create_time) FROM sys_job_log c WHERE a.job_name = c.job_name AND a.job_group = c.job_group)
WHERE NOT EXISTS (SELECT 1 FROM sys_job_log c WHERE a.job_name = c.job_name AND a.job_group = c.job_group AND c.create_time > b.create_time)
OR b.create_time IS NULL;
```
同时,需要在sys_job_log表的job_name、job_group、create_time字段上添加联合索引,以提高查询效率。
最后,建议定期清理sys_job_log表中的历史数据,以避免表数据过多导致查询效率下降。
import requests import os from bs4 import BeautifulSoup class book_spider(): def __init__(self,root_url): self.root_url=root_url self.book_list=[] #一级页面中获取的数据(二级页面地址)存放于此列表 self.chapter_list=[] #二级页面中获取的数据(三级页面地址和章节名)存放于此列表 def get_url(url): while True: try: res=requests.get(url) if res.status_code==200: res.encoding =res.apparent_encoding print("页面获取成功") return res.text else: print("页面返回异常",res.status_code) except: print("页面获取错误") def get_book_list(self.url): res = self.get_url(url) html = BeautifulSoup(res,"html.parser") a_list = html.find_all("a",{"class":"name"}) for a in a_list: self.book_list.append(a["href"]) self.book_list = [self.root_url+i for i in self.bbok_list] self.book_list.remove('http://10.1.88.252:7000/庆余年') print(book_list) def get_chapter_list(self,url): res = self.get_url(url) html = BeautifulSoup(res,"html.parser") a_list = html.find_all("a",{"class":"chapter"}) for a in a_list: self.chapter_list.append((a["href"],a.text.replace("\n",""))) def get_content(self.chapter): url = self.root_url + chapter[0] print(url) book_name = chapter[0].split("/")[1] print(book_name) if not os.path.exists(book_name): os.mkdir(book_name) res = self.get_url(url) html = BeautifulSoup(res,"html.parser") content = html.find("div",{"id":"content"}).text print(content) path = os.path.join(book_name,chapter[1]) with open(path,"w",encoding="utf8") as f: f.write(content) def main(): self.get_book_list(self.root_url) for book in self.book_list: self.get_chapter_liat(book) for chapter in chapter_list: self.get_content(chapter) book_s = book_spider("http://10.1.88.252:7000") book_s.main()这是一段爬虫代码,找出里面的错误并改正
有以下几个错误:
1. 在 `get_url` 方法中,需要在 `self` 参数之前添加 `@staticmethod` 装饰器,使其成为静态方法。
2. 在 `get_book_list` 方法中,`print(book_list)` 应该改为 `print(self.book_list)`,因为 `book_list` 是类成员变量。
3. 在 `get_book_list` 方法中,将 `self.bbok_list` 改为 `self.book_list`。
4. 在 `get_chapter_list` 方法中,将 `self.chapter_list.append((a["href"],a.text.replace("\n","")))` 改为 `self.chapter_list.append((self.root_url+a["href"],a.text.replace("\n","")))`,因为需要获取完整的章节链接。
5. 在 `get_content` 方法中,将 `print(url)` 和 `print(book_name)` 改为注释,因为这些是调试信息。
改正后的代码如下:
```
import requests
import os
from bs4 import BeautifulSoup
class book_spider():
def __init__(self,root_url):
self.root_url=root_url
self.book_list=[]
self.chapter_list=[]
@staticmethod
def get_url(url):
while True:
try:
res=requests.get(url)
if res.status_code==200:
res.encoding =res.apparent_encoding
print("页面获取成功")
return res.text
else:
print("页面返回异常",res.status_code)
except:
print("页面获取错误")
def get_book_list(self,url):
res = self.get_url(url)
html = BeautifulSoup(res,"html.parser")
a_list = html.find_all("a",{"class":"name"})
for a in a_list:
self.book_list.append(self.root_url + a["href"])
self.book_list.remove('http://10.1.88.252:7000/庆余年')
print(self.book_list)
def get_chapter_list(self,url):
res = self.get_url(url)
html = BeautifulSoup(res,"html.parser")
a_list = html.find_all("a",{"class":"chapter"})
for a in a_list:
self.chapter_list.append((self.root_url+a["href"],a.text.replace("\n","")))
def get_content(self,chapter):
url = chapter[0]
book_name = chapter[0].split("/")[1]
if not os.path.exists(book_name):
os.mkdir(book_name)
res = self.get_url(url)
html = BeautifulSoup(res,"html.parser")
content = html.find("div",{"id":"content"}).text
path = os.path.join(book_name,chapter[1])
with open(path,"w",encoding="utf8") as f:
f.write(content)
def main(self):
self.get_book_list(self.root_url)
for book in self.book_list:
self.get_chapter_list(book)
for chapter in self.chapter_list:
self.get_content(chapter)
book_s = book_spider("http://10.1.88.252:7000")
book_s.main()
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)