1 백만 개가 넘는 행이 있고 더 커질 수있는 데이터베이스 테이블이 있습니다. 이 데이터베이스 테이블에서 하나의 임의의 레코드를 잡을 데이터베이스를 쿼리하는 python 스크립트가 있습니다. 내가 사용하고있는 쿼리는 다음과 같습니다 :쿼리 속도 향상
SELECT *
FROM customers
WHERE cust_type = 'C'
ORDER BY RAND()
LIMIT 1;
더 좋은 방법이 있는지 궁금한가요? 그의 뛰어난 답변 마이클 벤자민에
감사 : 다음은 무작위 후 매우 빠른 오프셋 사용, 총을 기준으로 번호를 생성하는 그의 제안
def util_get_random_customer_individual():
# Gets a random customer from the MySQL DB Customers table. Users will need to parse items from the results into
# individual results
# Connect to the DB
config = {'user': 'user', 'password': 'password', 'host': 'host name',
'port': 3306, 'database': 'database'}
conn = mysql.connector.connect(**config)
c = conn.cursor()
type_code = 'I'
# Get a random customer based on the the count
c.execute('SELECT COUNT(*) FROM customers WHERE cust_type = %s', type_code)
count = c.fetchone()[0]
random_value = random.randint(1, count)
c.execute('SELECT * FROM customers WHERE cust_type = %s LIMIT %s, 1', (type_code, random_value,))
random_customer = c.fetchone()
return random_customer
가능한 중복 http://stackoverflow.com/questions/1244555/how-can-i-optimize-mysqls-order-by-rand-function) – krokodilko