Background: The association between sleep duration and cognition are inconclusive. Our study aimed to comprehensively investigate the effects of sleep duration on the risk of cognitive impairment in the middle-aged and older Chinese population. Methods: We used the data from four waves of the China Health and Retirement Longitudinal Study (CHARLS) cohort. Exposures included total sleep duration, nocturnal sleep duration, post-lunch napping, and changes in sleep duration over time. Cognitive function was assessed by a Chinese version of the Modified Mini-Mental State Examination (MMSE). Findings: We identified a U-shaped association of total sleep duration as well as nocturnal sleep duration with the risk of cognitive impairment among the included 7497 participants. 7-8 hours of total sleep duration and 6-7 hours of nocturnal sleep had the lowest risk of cognitive impairment. Further results showed that post-lunch napping within two hours was beneficial to cognition and 60 minutes was optimal. Moreover, analyses of changes in sleep duration further supported that sleeping less or more was a risk factor of cognitive impairment. Remarkably, those “excessive-change” sleepers (from ≤ 6 to ≥ 9 hours, or from ≥ 9 to ≤ 6 hours) had more risks. Interpretation: Keeping 7-8 hours per day in midlife and late life was related to the lowest risk of cognitive impairment, and an optimal post-lunch napping was 60 minutes for these stable sleepers. Excessive changes in sleep duration over time led to poorer cognition. These findings highlight the importance of optimal sleep habits to cognitive function. Funding Information: None. Declaration of Interests: We declare no competing interests Ethics Approval Statement: Ethical approval for the data collection of the CHARLS was obtained from the institutional review board of Peking University (IR1052-11015). All participants provided written informed consents during the investigation. We confirmed that all methods were conducted according to the relevant guidelines and regulations.